HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Selecting a specific row with a desired value

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withdesiredvalueselectingspecificrow

Problem

I am trying to get a row if it has the desired specific value, otherwise get the first or default:

dr =
   (from avTable in DAL.DataStore.DSet.Tables["Values1"].AsEnumerable()
    join pavTable in DAL.DataStore.DSet.Tables["Values2"].AsEnumerable()
        on avTable.Field("PK") equals pavTable.Field("FK_Value")
    where 
          pavTable.Field("FKI_ProductAttribute") == Value2 &&
          avTable.Field("Name") == "Specific Value"
    select avTable).FirstOrDefault() ??
    (from avTable in DAL.DataStore.DSet.Tables["Values1"].AsEnumerable()
     join pavTable in DAL.DataStore.DSet.Tables["Values2"].AsEnumerable()
     on avTable.Field("PK") equals pavTable.Field("FK_Value")
     where pavTable.Field("FKI_ProductAttribute") == Value2
     select avTable).FirstOrDefault();


This smells. Is there a better way to do this?

I want to do something like this:

var dt =
   (from avTable in DAL.DataStore.DSet.Tables["Values1"].AsEnumerable()
    join pavTable in DAL.DataStore.DSet.Tables["Values2"].AsEnumerable()
        on avTable.Field("PK") equals pavTable.Field("FK_Value")
    where 
          pavTable.Field("FKI_ProductAttribute") == Value2 &&
          avTable.Field("Name") == "Specific Value"
    select avTable)

DataRow dr = dt.Select("PK = Specified value");
If(dr==null) dr = dt.FirstOrDefault();


and then do a select based on a specified value. If that is null, then grab the first or default. This seems to be more DRY, but is it more or less efficient?. Also, I have a hard time doing a select on the dt based on the specific value (lack of Linq expertise and Google prowess to find the answer I am looking for).

Solution

Will this work?

var dr= (from avTable in DAL.DataStore.DSet.Tables["Values1"].AsEnumerable()
         join pavTable in DAL.DataStore.DSet.Tables["Values2"].AsEnumerable()
             on avTable.Field("PK") equals pavTable.Field("FK_Value");
         where
             (pavTable.Field("FKI_ProductAttribute") == Value2 &&
              avTable.Field("Name") == "Specific Value") ||
              pavTable.Field("FKI_ProductAttribute") == Value2
         select avTable)
        .FirstOrDefault();


I don't really have a way to test this, but I believe if the first part of the where clause criteria is met, the part after the || is skipped. If the first part returns no record then the second part is evaluated and will return a result ...or null.

Code Snippets

var dr= (from avTable in DAL.DataStore.DSet.Tables["Values1"].AsEnumerable()
         join pavTable in DAL.DataStore.DSet.Tables["Values2"].AsEnumerable()
             on avTable.Field<int>("PK") equals pavTable.Field<int>("FK_Value");
         where
             (pavTable.Field<int>("FKI_ProductAttribute") == Value2 &&
              avTable.Field<string>("Name") == "Specific Value") ||
              pavTable.Field<int>("FKI_ProductAttribute") == Value2
         select avTable)
        .FirstOrDefault();

Context

StackExchange Code Review Q#4552, answer score: 2

Revisions (0)

No revisions yet.