Lambda表达式中查找日期在另一个日期列表中的列表项的最佳方法

huangapple go评论77阅读模式
英文:

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).

我需要这些日期值是我接下来要获取的列表中的值("x.Date" 值)。

var response = myObject
               .Where(x => x.UserId == userId
                            && dates.Contains(x.Date))
               .Select(x => new
               {
                   x.Id,
                   x.Quantity,
                   x.Date
               })
               .OrderBy(x => x.Id)
               .ToList();
英文:

I have a list of dates:

List&lt;DateTime&gt; 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 =&gt; xUserId == userId
               &amp;&amp; Date = x.Date == //this must be the dates values 
               .Select(x =&gt; new
               {
                   x.Id,
                   x.Quantity,
                   x.Date
               })
               .OrderBy(x =&gt; x.Id)
               .ToList();

答案1

得分: 1

Use Where ... Contains:

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

Use Where ... Contains:

var response = myObject
    .Where(x =&gt; xUserId == userId &amp;&amp; dates.Contains(x.Date))
    ...

huangapple
  • 本文由 发表于 2023年5月30日 00:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定