簡體   English   中英

如何基於該對象的兩個成員的值從通用列表中檢索該對象?

[英]How can I retrieve an object from a generic list based on the value of two of that object's members?

我可以從該對象的通用列表中檢索該對象,如下所示:

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(*);

后者也不起作用,但是哪個方向是正確的方向或其他方向?

此處的謂詞只是簡單地解析為bool來確定是否匹配:

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

所以任何解析為一個bool也有同樣的效果:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM