简体   繁体   English

从列表中查找丢失的ID <int> 在数据表中

[英]Find Missing IDs From List<int> in DataTable

I have a List and a DataTable which contains a column to match the IDs in the list. 我有一个列表和一个数据表,其中包含一列以匹配列表中的ID。 I need to identify all IDs in my list that are not in the DataTable. 我需要标识列表中所有不在DataTable中的ID。 I tried getting an IEnumberable DataRow and joining that to the list but I'm not able to identify the missing ones. 我尝试获取IEnumberable DataRow并将其加入列表,但无法识别丢失的。

Here is my code and what I've tried... 这是我的代码以及我尝试过的内容...

List<int> JobIdList = (from i in EntryItems select i.JobID.Value).ToList<int>();
IEnumerable<DataRow> rowInfo = JobBLL.JobsExist(JobIdList).AsEnumerable();

var MissingList = (from rec in rowInfo
                    join id in JobIdList
                    on rec.Field<int>("JobID") equals id
                    into grouping
                    from id in grouping.DefaultIfEmpty()
                    select new { id }).ToList();

if (MissingList.Count > 0) { // Show message and exit }

The problem is that this returns the items in the data table that ARE found. 问题是这将返回在数据表中找到的项目。 Let's say I have 1, 2, and 3 in my list but my data table only has 1 and 3. I want to have 2 in MissingList. 假设我的列表中有1、2和3,但是我的数据表中只有1和3。我想在MissingList中有2。

Any thoughts? 有什么想法吗?

var jobIdList = new HashSet<int>(from i in EntryItems select i.JobID.Value);
var rows = JobBLL.JobsExist(jobIdList).AsEnumerable();

var foundIds = (from x in rows select x.Field<int>("JobID")).ToList();
var missingIds = jobIdList.Except(foundIds);

您需要将以下代码行添加到您的代码中。

var missingIds = JobIdList.Except(MissingList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM