【问题标题】:How can I retrieve an object from a generic list based on the value of two of that object's members?如何根据对象的两个成员的值从通用列表中检索对象?
【发布时间】:2015-11-10 19:14:05
【问题描述】:

我可以像这样从该对象的通用列表中检索一个对象:

return _itemTotalsAcrossMonthsList.Find(s => s.ItemDescription == desc);

但是当我需要搜索多个对象成员值(在我的例子中是两个)时,如何从通用列表中检索对象。我从这个开始:

ItemsForMonthYear ifmy;
. . .
ifmy = _itemsForMonthYearList.Find(s => s.ItemDescription == itemDesc);

...但我还需要根据monthYr 中的值进行搜索。我希望它会像这样明显:

ifmy = _itemsForMonthYearList.Find(s => s.ItemDescription == itemDesc, t => t.monthYr == monYr);

我需要做类似的事情吗:

ifmy = from _itemsForMonthYearList.
       Where (ItemDescription == itemDesc) && (monthYr == monYr).
       Select(*);

?后者也不起作用,但哪个方向是正确的,还是别的什么?

【问题讨论】:

    标签: c# linq generics lambda


    【解决方案1】:

    这里的谓词只是简单地解析为bool 来确定是否匹配:

    Find(s => s.ItemDescription == itemDesc)
    

    所以任何解析为bool 的东西都会产生相同的效果:

    Find(s => s.ItemDescription == itemDesc && s.monthYr == monYr)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      相关资源
      最近更新 更多