C# Linq查本集合中不包含于另一个集合的元素
阅读原文时间:2023年07月09日阅读:2

public void Linq() {
List products = GetProductList();
List customers = GetCustomerList();

var productFirstChars =  
    from prod in products  
    select prod.ProductName\[0\];  
var customerFirstChars =  
    from cust in customers  
    select cust.CompanyName\[0\];

**var productOnlyFirstChars =** **productFirstChars.Except(customerFirstChars);** 

  //筛选表所有修改的数据(差集)(同样适用于DataTable)

  //DataTable dtResult = dtAllData.AsEnumerable().Except(dtPartData.AsEnumerable());

Console.WriteLine("First letters from Product names, but not from Customer names:"); foreach (var ch in productOnlyFirstChars) { Console.WriteLine(ch); } }