2012-10-04 45 views
1

我有一個要求,我有一個字符串[]數組(它具有的ID值是分離值)和DT作爲數據表。傳遞列表值在LINQ條件中用於過濾數據中的數據

dt有名爲Id,empname,名稱的列。

現在我想使用linq查詢過濾數據表中的數據,其中id不在(字符串[]值)。

爲前:

string[] ids= [2,4,6]; 

dt= id  empname  designation 
    ----  -------  ------------ 
    1  robert  trainer 
    2  thomas  HRA 
    3  John   JE 
    4  kapil  SE 
    5  sachin  SSE 
    6  Rahul  Manager 

現在我想LINQ查詢這回我的DT爲:

id  empname  designation 
----  -------  ------------ 
1  robert  trainer 
3  John   JE 
5  sachin  SSE 

回答

1

您可以使用LINQ到數據表:

var result = dt.AsEnumerable() 
       .Where(row => !ids.Contains(row.Field<string>("Id")); 
+0

'ids'是一個'string'數組。 – Magnus

+0

@Magnus:謝謝,編輯 –

+0

+1此外,爲了獲得更好的性能,您可以考慮將'ids'數組添加到'HashSet'並將其用於'Where'中的查找。 – Magnus

相關問題