C# 如何从datatable中选择不在IEnumerable中的数据

C# 如何从datatable中选择不在IEnumerable中的数据,c#,asp.net,linq,datatable,ienumerable,C#,Asp.net,Linq,Datatable,Ienumerable,我有一个DataTabledbcrs,我只想在以下可枚举列表中获取而不是的数据: IEnumerable<Crs> res IEnumerable res 注意:两者中的键都是id首先需要使用AsEnumerable()来查询数据表的行集合,然后使用!包含的与此不同: var query = from r in dbcrs.AsEnumerable() where !( from s in res select r.Id) .Cont

我有一个DataTable
dbcrs
,我只想在以下可枚举列表中获取而不是的数据:

IEnumerable<Crs> res
IEnumerable res

注意:两者中的键都是
id
首先需要使用
AsEnumerable()
来查询数据表的行集合,然后使用
!包含的
与此不同:

var query = from r in dbcrs.AsEnumerable()
        where !( from s in res select r.Id)
               .Contains(r.Id)
        select r;

首先,您需要使用
AsEnumerable()
来查询DataTable的Rows集合,然后使用
!包含的
与此不同:

var query = from r in dbcrs.AsEnumerable()
        where !( from s in res select r.Id)
               .Contains(r.Id)
        select r;
以下是我的建议:

var result = dbcrs.Where(item => res.FirstOrDefault(resItem => resItem.Id == item.Id) == null);
以下是我的建议:

var result = dbcrs.Where(item => res.FirstOrDefault(resItem => resItem.Id == item.Id) == null);

使用Except和IEquatable执行此操作的示例

这种方法的一个好处是,您可以定义“Equals”的含义,这样两个可能具有相同ID但不相等的列表仍然可以使用

e、 g.您从两个表中获取数据,因此Id可以重复,但其他一些属性定义它们是否实际相等

class Crs:IEquatable<Crs>
        {
            public int Id { get; set; }
            public string Description { get; set; }

            public bool Equals(Crs other)
            {
                if (Object.ReferenceEquals(other, null)) 
                    return false;

                if (Object.ReferenceEquals(this, other)) 
                    return true;

                return Id.Equals(other.Id) && Description.Equals(other.Description);
            }

            public override int GetHashCode()
            {
                int hashId = Id.GetHashCode();
                int hashDescription = Description == null ? 0 : Description.GetHashCode();
                return hashId ^ hashDescription;
            }

        }

        internal static void RunMe()
        {
            var dataTable = new List<Crs>(){
                new Crs{Id=1, Description="First"},
                new Crs{Id=2, Description="Second"},
                new Crs{Id=5, Description="Fifth"}
            };

            var enumerable = new List<Crs>(){
                new Crs{Id=2, Description="Second"},
                new Crs{Id=4, Description="Fourth"}
            };

            var distinct = dataTable.Except(enumerable);

            distinct.ToList().ForEach(d => Console.WriteLine("{0}: {1}", d.Id, d.Description));
        }
类别Crs:IEquatable
{
公共int Id{get;set;}
公共字符串说明{get;set;}
公共布尔等于(Crs其他)
{
if(Object.ReferenceEquals(other,null))
返回false;
if(Object.ReferenceEquals(this,other))
返回true;
返回Id.Equals(其他.Id)和&Description.Equals(其他.Description);
}
公共覆盖int GetHashCode()
{
int hashId=Id.GetHashCode();
int hashDescription=Description==null?0:Description.GetHashCode();
返回hashId^hashDescription;
}
}
内部静态void RunMe()
{
var dataTable=新列表(){
新的Crs{Id=1,Description=“First”},
新的Crs{Id=2,Description=“Second”},
新的Crs{Id=5,Description=“Fifth”}
};
var enumerable=新列表(){
新的Crs{Id=2,Description=“Second”},
新的Crs{Id=4,Description=“Fourth”}
};
var distinct=dataTable.Except(可枚举);
distinct.ToList().ForEach(d=>Console.WriteLine(“{0}:{1}”,d.Id,d.Description));
}

使用Except和IEquatable执行此操作的示例

这种方法的一个好处是,您可以定义“Equals”的含义,这样两个可能具有相同ID但不相等的列表仍然可以使用

e、 g.您从两个表中获取数据,因此Id可以重复,但其他一些属性定义它们是否实际相等

class Crs:IEquatable<Crs>
        {
            public int Id { get; set; }
            public string Description { get; set; }

            public bool Equals(Crs other)
            {
                if (Object.ReferenceEquals(other, null)) 
                    return false;

                if (Object.ReferenceEquals(this, other)) 
                    return true;

                return Id.Equals(other.Id) && Description.Equals(other.Description);
            }

            public override int GetHashCode()
            {
                int hashId = Id.GetHashCode();
                int hashDescription = Description == null ? 0 : Description.GetHashCode();
                return hashId ^ hashDescription;
            }

        }

        internal static void RunMe()
        {
            var dataTable = new List<Crs>(){
                new Crs{Id=1, Description="First"},
                new Crs{Id=2, Description="Second"},
                new Crs{Id=5, Description="Fifth"}
            };

            var enumerable = new List<Crs>(){
                new Crs{Id=2, Description="Second"},
                new Crs{Id=4, Description="Fourth"}
            };

            var distinct = dataTable.Except(enumerable);

            distinct.ToList().ForEach(d => Console.WriteLine("{0}: {1}", d.Id, d.Description));
        }
类别Crs:IEquatable
{
公共int Id{get;set;}
公共字符串说明{get;set;}
公共布尔等于(Crs其他)
{
if(Object.ReferenceEquals(other,null))
返回false;
if(Object.ReferenceEquals(this,other))
返回true;
返回Id.Equals(其他.Id)和&Description.Equals(其他.Description);
}
公共覆盖int GetHashCode()
{
int hashId=Id.GetHashCode();
int hashDescription=Description==null?0:Description.GetHashCode();
返回hashId^hashDescription;
}
}
内部静态void RunMe()
{
var dataTable=新列表(){
新的Crs{Id=1,Description=“First”},
新的Crs{Id=2,Description=“Second”},
新的Crs{Id=5,Description=“Fifth”}
};
var enumerable=新列表(){
新的Crs{Id=2,Description=“Second”},
新的Crs{Id=4,Description=“Fourth”}
};
var distinct=dataTable.Except(可枚举);
distinct.ToList().ForEach(d=>Console.WriteLine(“{0}:{1}”,d.Id,d.Description));
}
DataTable dt=newdatatable();
dt.Columns.AddRange(新数据列[])
{
新数据列(“Id”,类型(System.Int32)),
新数据列(“名称”,类型(System.String))
});
dt.Rows.Add(新对象[]{1,“Test”});
Add(新对象[]{2,“Test”});
var l=新的Int32[]{2,4};
var l1=dt.AsEnumerable()。其中(p1=>Array.IndexOf(l,p1.Field(0))
DataTable dt=newdatatable();
dt.Columns.AddRange(新数据列[])
{
新数据列(“Id”,类型(System.Int32)),
新数据列(“名称”,类型(System.String))
});
dt.Rows.Add(新对象[]{1,“Test”});
Add(新对象[]{2,“Test”});
var l=新的Int32[]{2,4};
var l1=dt.AsEnumerable().Where(p1=>Array.IndexOf(l,p1.Field(0))
dbcrs.Where(item=>res.FirstOrDefault(resItem=>resItem.Id.TrimEnd()==item。[0].ToString().TrimEnd())==null);
dbcrs.Where(item=>resItem=>resItem.Id.TrimEnd()=item.=null)