2013-11-22 80 views
0

可以查詢子句需要識別ID或者我應該說的id組集合中,可以查詢是這樣如何實現「在哪裏」在LINQ

var logs = from record in RecordCollection(string.Concat(sourcePath + fileName)) 
          where record.Id == 800 && record.Id == 307 && record.Id == 314 && record.Id == 372 && record.Id == 105 
          select record; 

這是正確的方式?即使系列中有ID,我也沒有得到任何結果。 TIA

回答

2

||

var logs = from record in RecordCollection(string.Concat(sourcePath + fileName)) 
      where record.Id == 800 || 
       record.Id == 307 || 
       record.Id == 314 || 
       record.Id == 372 || 
       record.Id == 105 
      select record; 

或更短這樣的替換&&

var ids = new []{800,307,314,372,105}; 
var logs = from record in RecordCollection(string.Concat(sourcePath + fileName)) 
      where ids.Contains(record.Id) 
      select record; 
+0

謝謝,我已經採取了更短的一個。 –

+0

@KGSosa是的,這是更好的,還記得接受答案,謝謝。 –

1

你可能想的||(OR)運算符:

var logs = from record in RecordCollection(string.Concat(sourcePath + fileName)) 
      where record.Id == 800 || 
       record.Id == 307 || 
       record.Id == 314 || 
       record.Id == 372 || 
       record.Id == 105 
      select record; 

現在你檢查你當前正在迭代的記錄ts Id具有值800和307和314等您想檢查它是否有任何這些。

+0

是的,我想檢查這些ID是否在列表中 –

+0

@KGSosa你還在等什麼?更好的答案? –

0

考慮下面的代碼...

var logs = from record in RecordCollection(string.Concat(sourcePath + fileName)) 
         where new List<int> { 800, 307, 314, 372, 105}.Contains(record.Id) 
         select record; 

祝您好運!

0

&&替換爲||

要在許多地方使用,你應該建立一個方法。例如這裏:

public static bool IsIn<T>(this T source, params T[] values) 
{ 
    return values.Contains(source); 
} 

,並稱之爲:

var logs = from record in RecordCollection(string.Concat(sourcePath + fileName)) 
         where record.IsIn(800,105,307) 
         select record;