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

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

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

问题

I have a list of dates:

我有一个日期列表:

  1. 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" 值)。

  1. var response = myObject
  2. .Where(x => x.UserId == userId
  3. && dates.Contains(x.Date))
  4. .Select(x => new
  5. {
  6. x.Id,
  7. x.Quantity,
  8. x.Date
  9. })
  10. .OrderBy(x => x.Id)
  11. .ToList();
英文:

I have a list of dates:

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

  1. var response = myObject
  2. .Where(x =&gt; xUserId == userId
  3. &amp;&amp; Date = x.Date == //this must be the dates values
  4. .Select(x =&gt; new
  5. {
  6. x.Id,
  7. x.Quantity,
  8. x.Date
  9. })
  10. .OrderBy(x =&gt; x.Id)
  11. .ToList();

答案1

得分: 1

Use Where ... Contains:

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

Use Where ... Contains:

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

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:

确定