左连接列表<t> with Datatable -Linq </t>

时间:2013-10-23 00:03:12

标签: c# .net linq

我有List<t>DataTable。通过使用Linq,我想检索List<Product>中不存在的产品列表DataTable

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
                 };

1 个答案:

答案 0 :(得分: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)));