Best way to find list items whose date is in another date list in Lambda Expression

I have a list of dates:

List<DateTime> dates;

And I need those date values ​​to be the ones in the following list that I am going to get ("x.Date" value).

var response = myObject
               .Where(x => xUserId == userId
               && Date = x.Date == //this must be the dates values 
               .Select(x => new
               {
                   x.Id,
                   x.Quantity,
                   x.Date
               })
               .OrderBy(x => x.Id)
               .ToList();

>Solution :

Use WhereContains:

var response = myObject
    .Where(x => xUserId == userId && dates.Contains(x.Date))
    ...

Leave a Reply