我有以下的比較對象一個子項的屬性:如何在2只列出了LINQ
public interface ITray
{
int OrderNo {get; set;}
IEnumerable<ITrayItem> TrayItems {get;}
}
public interface ITrayItem
{
int Aisle {get; set;}
}
現在,我有兩個表的對象,
List<ITray> selectedTrays
List<ITray> poolTrays
我所試圖做的是對於poolTrays中的每個元素,我想比較所選紙盤列表中的通道。如果所有的通道匹配,我想將它添加到要返回的托盤列表中。 我只是在試圖讓linq與查詢列表中的集合的屬性並返回匹配的列表中的項目打結。
這是我的時刻:
List<int> selectedAisles = (from tray in selectedTrays
from item in tray.TrayItems
select item.Aisle).Distinct().ToList()
List<ITray> trayswithMatchingAisles =
(from t in poolTrays
from item in t.TrayItems
where selectedAisles.Contains(item.Aisle)
select t).ToList();
所以,如果我已經選擇托盤A,B,C與在括號過道 A [1,2,3] B [4,5 ,6] c [7,8,9]
然後在aisles [7,9]中使用TrayItems的poolTray應該成功返回,但不應返回帶有TrayItems [7,8,9,10]的池托盤在列表中。
目前,我傳遞(只)[7,9]在我poolTray名單,和它的兩個實例都在我的LINQ查詢返回
對不起,剛剛意識到一個不是c。 – jazza1000
乾杯pswg,你的答案對我來說是訣竅 – jazza1000
@ jazza1000你能測試我的答案嗎?它工作嗎? (剛剛編輯)。 –