2012-09-14 29 views
0

我有一個包含日期時間項目列表的對象。我有另一個列表,其中有一個屬性我將如何使用LINQ做以下邏輯

我想在列表中選擇對象中的日期時間項目,只有當它匹配另一個列表中的其中一個項目時。我能夠獲得項目,但我不知道如何基本寫入「如果當前項目與此列表中的任何項目匹配」。

的LINQ到目前爲止就像

from item in ObjectWithList.DateList 
from compareItem in OtherDateTimeList 
where item = //Here is there I run into trouble, how would I loop through the compareitems? 

感謝

編輯 我需要在這LINQ完成這件事,因爲這僅僅是一個在所有LINQ的部分。

回答

1
ObjectWithList.DateList.Intersect(OtherDateTimeList) 

編輯

如果必須是一個LINQ查詢,你不希望使用相交,試試這個:

var mix = from f in ObjectWithList.DateList 
      join s in OtherDateTimeList on f equals s 
      select f; 

var mix = from f in ObjectWithList.DateList 
      from s in OtherDateTimeList 
      where f == s 
      select f; 
+0

我需要在此LINQ中完成此操作,因爲這只是整個LINQ的一部分。 –

0

你可以使用Intersect標準查詢運算符:

var items = ObjectWithList.DateList.Intersect(OtherDateTimeList) 
+0

我需要在此LINQ中完成此操作,因爲這只是整個LINQ的一部分。 –