简体   繁体   中英

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.

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

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 {.....};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM