gpt4 book ai didi

c# - Left Join List with Datatable -Linq

转载 作者:行者123 更新时间:2023-11-30 16:15:32 29 4
gpt4 key购买 nike

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

列表product ( List<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是我需要的 fiddler 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
};

最佳答案

试试这个:

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

关于c# - Left Join List<t> with Datatable -Linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530452/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com