Linq IList和datatable之间的声明

时间:2013-04-24 18:52:22

标签: c# linq entity-framework linq-to-entities

我有一个IList,我通过linq查询到一个表中的实体。通过其他方式,我得到了一个数据表,代表数据库中的表。两个表都有相同的列。我想搜索IList中可用但不在数据表中的EmployeeID。请建议使用linq语句如何做到这一点。我在网上搜索并发现了许多条款,但我仍然很困惑如何做到这一点。

例如我在帖子上找到了这段代码

from c in db.Customers
where !db.Products.Any(p => p.ProductID == c.ProductID)
select c;

1 个答案:

答案 0 :(得分:2)

假设这些:

DataTable dt; //your datatable

var results = from c in db.Customers
              where !db.Products.Any(p => p.ProductID == c.ProductID)
              select c;

然后你可以这样做:

var ids = results
    .Where(r => !dt.Rows.AsEnumerable
        .Any(d => d.ItemArray["EmployeeID"] == r.EmployeeID)
    .Select(r => r.EmployeeID);