xml数据类型不能选择为DISTINCT,因为它是不可比较的错误

发布于 2025-01-08 18:47:53 字数 514 浏览 1 评论 0原文

我的代码中有此查询,我收到此错误。

 var auditMandate = (from ae in genDB.AuditEvent
                     join at in genDB.AuditTable on // snip
                     select ae)
                     .OrderByDescending(x => 
                         x.DateTime_Updated).Take(500)
                     .Distinct().ToList();

执行命令定义时发生错误。有关详细信息,请参阅内部异常。
xml 数据类型不能选择为 DISTINCT,因为它不具有可比性。 xml 数据类型不能选择为 DISTINCT,因为它不具有可比性。

这是我在 Linq 做错了什么吗?

谢谢

i have this query in my code I am getting this Error.

 var auditMandate = (from ae in genDB.AuditEvent
                     join at in genDB.AuditTable on // snip
                     select ae)
                     .OrderByDescending(x => 
                         x.DateTime_Updated).Take(500)
                     .Distinct().ToList();

An error occurred while executing the command definition. See the inner exception for details.
The xml data type cannot be selected as DISTINCT because it is not comparable. The xml data type cannot be selected as DISTINCT because it is not comparable.

is that something I am doing wrong this Linq?

Thanks

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

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

发布评论

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

评论(2

心的位置 2025-01-15 18:47:54

您尝试选择不同的 AuditEvent,但 LINQ 提供程序不知道如何确定两个 AuditEvent 是否相同。考虑投射到一个知道如何进行这种比较的类中。另一种选择是使用 GroupBy 来根据特定属性生成不同的项目:

.GroupBy(ae => ae.Id).Select(g => g.First());

顺便说一句,在调用 Take 之前进行不同的检查通常更有意义,因此您将即使原始集中的前 500 个项目包含重复项,仍然可以获得最多 500 个项目。

You're trying to select distinct AuditEvents, but your LINQ provider doesn't know how to determine whether two AuditEvents are the same. Consider projecting into a class that knows how to do this comparison. Another option would be to use a GroupBy to produce items that are distinct based on specific properties:

.GroupBy(ae => ae.Id).Select(g => g.First());

As a side note, it generally makes more sense to do the distinct check before you call Take, so you'll still get up to 500 items even if the first 500 items in the original set contain duplicates.

混吃等死 2025-01-15 18:47:53

这是可行的。接受您的查询,但去掉 Distinct 调用。不过,您仍然需要一个具体的列表,因此请保留 ToList()。然后按照这个想法(因为我不知道你的xml结构):

我的表

CREATE TABLE [dbo].[XmlTable](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [data] [xml] NULL
)

我的数据

insert into XmlTable
values('<list id=''a''><items></items></list>')
insert into XmlTable
values('<list id=''b''><items></items></list>')
insert into XmlTable
values('<list id=''a''><items></items></list>')

我的比较器

    public class MyXDocumentCoparer : IEqualityComparer<XDocument>
    {
        public bool Equals(XDocument x, XDocument y)
        {
            var xId = x.Root.Attribute("id").Value;
            var yId = y.Root.Attribute("id").Value;
            return xId == yId;
        }

        public int GetHashCode(XDocument obj)
        {
            var id = obj.Root.Attribute("id").Value;
            return id.GetHashCode();
        }
    }

我的代码

    using (var ctx = new xmltestEntities())
    {
        // this would be your concrete list
        var rawData = ctx.XmlTables.ToArray();

        var processedData = rawData
            .Select(row => XDocument.Parse(row.data))
            .Distinct(new MyXDocumentCoparer());

        // you'll only get two, boom!
        foreach (var item in processedData)
            Console.WriteLine(item.Root.Attribute("id"));

        Console.ReadLine();
    }

This is doable. Take the query you have but get rid of the Distinct call. You still want a concrete list however so keep ToList(). Then follow this idea (since I don't know your xml structure):

My Table

CREATE TABLE [dbo].[XmlTable](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [data] [xml] NULL
)

My Data

insert into XmlTable
values('<list id=''a''><items></items></list>')
insert into XmlTable
values('<list id=''b''><items></items></list>')
insert into XmlTable
values('<list id=''a''><items></items></list>')

My Comparer

    public class MyXDocumentCoparer : IEqualityComparer<XDocument>
    {
        public bool Equals(XDocument x, XDocument y)
        {
            var xId = x.Root.Attribute("id").Value;
            var yId = y.Root.Attribute("id").Value;
            return xId == yId;
        }

        public int GetHashCode(XDocument obj)
        {
            var id = obj.Root.Attribute("id").Value;
            return id.GetHashCode();
        }
    }

My Code

    using (var ctx = new xmltestEntities())
    {
        // this would be your concrete list
        var rawData = ctx.XmlTables.ToArray();

        var processedData = rawData
            .Select(row => XDocument.Parse(row.data))
            .Distinct(new MyXDocumentCoparer());

        // you'll only get two, boom!
        foreach (var item in processedData)
            Console.WriteLine(item.Root.Attribute("id"));

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