2013-10-23 43 views
3

我有一個List<t>DataTable。通過使用Linq,我想檢索DataTable中不存在的產品List<Product>的列表。左加入列表<t>與Datatable -Linq

productList<Product>)名單

Number TechnicalDescription 
1   This is the A item 
2   This is the B item 
3   This is the Z item 

Name   Description 
A   This is the A item 
B   This is the B item 
C   This is the C item 
D   This is the D item 

結果:List<Product>

Number TechnicalDescription 
3   This is the Z item 

Here是我所需要的提琴手SQL的例子,但使用LINQ。

我試過這個,但不起作用。

List<Product> productList = THE_LIST; 
DataTable dtProducts = THE_TABLE; 

var myNewList = from e in productList 
       join p in dtProducts.AsEnumerable() 
        on e.TechnicalDescription.ToLower() 
        equals p.Field<string>("Description").ToLower() 
        into productGroup 
       from p in productGroup.DefaultIfEmpty(new { ID = "0", TechnicalDescription = "" }) 
       where p != null 
       select new 
       { 
        ID = e.ID, 
        TechnicalDescription = e.TechnicalDescription 
       }; 

回答

3

試試這個:

var table = 
    dtProducts.AsEnumerable() 
     .Select(r => p.Field<string>("Description")).ToList(); 
var result = 
    productList 
     .Where(p => !table.Any(t => StringComparer.OrdinalIgnoreCase.Equals(t, p.TechnicalDescription))); 
+2

的最佳解決方案,只有一個建議'等於(T,p.TechnicalDescription)' – Coyolero

+0

@Coyolero,好,謝謝。 –