Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have this linq query:

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"
    select avTable).FirstOrDefault() ??
    (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
     select avTable).FirstOrDefault();

I am trying to get a row if it has the desired specific value. Otherwise get the first or default.

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

Clarification - 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<int>("PK") equals pavTable.Field<int>("FK_Value")
    where 
          pavTable.Field<int>("FKI_ProductAttribute") == Value2 &&
          avTable.Field<string>("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...I don't know. 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).

share|improve this question
    
This is the way FirstOrDefault is supposed to work ...? I don't see anything that 'smells' ...unless there is some optimization to the query. But nothing I can really see ... –  IAbstract Sep 2 '11 at 20:50
    
It just doesn't seem DRY to me, so I clarified what I am looking to do. –  Ben Jones Sep 2 '11 at 21:10
    
'But nothing I can really see ...' ...and then I put my glasses on ... –  IAbstract Sep 2 '11 at 23:04

1 Answer 1

up vote 1 down vote accepted

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<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();

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.