frenchie
frenchie

Reputation: 51937

linq query with condition

I'm writing a linq query in query syntax and I'm wondering how to add another where clause. Basically, I have the following:

var test = from t in MyDC.TheTable
           where t.UserID == TheUserID
           where t.DateDone.Date == TheDate.Date
           select new MyModel {.....};

TheTable has a column called LinkedID and this column is also in another table called ColorStatus (a number between 1 and 10). I'm looking to write the where clause "where the LinkedID in the ColorStatus table is less than 7".

Thanks.

Upvotes: 0

Views: 461

Answers (2)

Cheng Chen
Cheng Chen

Reputation: 43523

Your information "another table called ColorStatus" doesn't make sense here.

var test = from t in MyDC.TheTable
           where t.UserID == TheUserID
              && t.DateDone.Date == TheDate.Date
              && t.LinkedID < 7           
           select new MyModel {.....};

Probably I didn't get your idea, here is an example of join may help you.

var test = from t in MyDC.TheTable
           join x in MyDC.ColorStatus
           on t.LinkedID == x.LinkedID
           where t.UserID == TheUserID
              && t.DateDone.Date == TheDate.Date
              && x.AnotherField == 1
           select new MyModel {.....};

Upvotes: 2

Fun Mun Pieng
Fun Mun Pieng

Reputation: 6891

Just a suggestion on improving the statement you have. You can actually merge the two where conditions into a single one. && means "AND"

Where t.UserID == TheUserID && t.DateDone.Date = TheDate.Date

Upvotes: 4

Related Questions