如何从数据表中选择不在 IEnumerable 中的数据

发布于 2024-12-22 09:35:43 字数 185 浏览 1 评论 0原文

我有一个 DataTable dbcrs,我只想获取以下枚举中的数据:

IEnumerable<Crs> res

注意:两者的键都是 <代码>id。

I have a DataTable dbcrs and I want to get only the data which is not in the following enumerable:

IEnumerable<Crs> res

Note : the key in both is id.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哀由 2024-12-29 09:35:43

这是我的建议:

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

Here is my suggestion:

var result = dbcrs.Where(item => res.FirstOrDefault(resItem => resItem.Id == item.Id) == null);
半葬歌 2024-12-29 09:35:43

首先,您需要使用 AsEnumerable() 来查询 DataTable 的 Rows 集合,然后使用 !Contains as not in ,如下所示:

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

First you need to use AsEnumerable() in order to query against the DataTable's Rows collection, then use !Contains as not in like this:

var query = from r in dbcrs.AsEnumerable()
        where !( from s in res select r.Id)
               .Contains(r.Id)
        select r;
三月梨花 2024-12-29 09:35:43

使用 except 和 IEquatable<> 执行此操作的示例如下:

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

例如,您从两个表中获取数据,因此 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));
        }

An example of doing this with Except and IEquatable<>

A benefit of this way is that you can define what you mean by "Equals", so that two lists which may have the same ID's but are NOT equal can still be used.

e.g. You get data from two tables, so the Id's can repeat but some other properties define if they are actually equal.

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));
        }
紫南 2024-12-29 09:35:43
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[]
        {
            new DataColumn("Id", typeof(System.Int32)),
            new DataColumn("Name", typeof(System.String))

        });

        dt.Rows.Add (new Object[]{1,"Test"});
        dt.Rows.Add(new Object[] {2, "Test" });



        var l = new Int32[] {  2, 4 };


        var l1 = dt.AsEnumerable().Where(p1 => Array.IndexOf(l, p1.Field<Int32>(0))<0).CopyToDataTable();

这将返回一行,因为在数据表和数组中都有一个共同的值,即 2。所以输出将是

2,测试

        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[]
        {
            new DataColumn("Id", typeof(System.Int32)),
            new DataColumn("Name", typeof(System.String))

        });

        dt.Rows.Add (new Object[]{1,"Test"});
        dt.Rows.Add(new Object[] {2, "Test" });



        var l = new Int32[] {  2, 4 };


        var l1 = dt.AsEnumerable().Where(p1 => Array.IndexOf(l, p1.Field<Int32>(0))<0).CopyToDataTable();

This would return us one row because in Datatable and array both have one value in common that's 2 only. so out put will be

2, Test

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文