错误:“无法将 xml 数据类型选择为 DISTINCT,因为它不可比较。”

发布于 2024-12-05 07:48:06 字数 1560 浏览 2 评论 0原文

在我的代码中,我有以下 Linq 查询:

IQueryable<Data> charts = (from report in ctx.Charts group report by new 
    { 
        Name = report.ChartTitle.ChartType.ChartCategory.CategoryName,
        id = report.ChartTitle.ChartType.ChartCategory.ChartCategoryId, 
        Period = report.Period 
    } into d
    select new Data
    {
        Name = d.Key.Name,
        Id = d.Key.id,
        Period = d.Key.Period,            
        Reports = from r in d group r by new 
        { Title = r.ChartTitle.Name, id = r.ChartTitle.ChartTitleId } into rs
        select new Report
        {
            Title = rs.Key.Title,
            Id = rs.Key.id,
            Charts = (from c in rs group c by new 
                    { 
                        ChartId = c.ChartId, 
                        FiscalYear = c.FiscalYear, 
                        ModifiedDate = c.ChartView.ModifiedDate, 
                        Function = c.Function.DisplayName, 
                        ChartData=c.ChartView.ViewData
                    } into cs
                    select new ChartInfo 
                    { 
                        ChartId = cs.Key.ChartId, 
                        FiscalYear = cs.Key.FiscalYear,
                        ModifiedDate = cs.Key.ModifiedDate, 
                        Function = cs.Key.Function, 
                        ChartData=cs.Key.ChartData 
                    })
    }});

在上面的代码中,如果我排除“ChartData”字段(属于 XML 数据类型),则查询执行正常。但是,当我包含此字段时,它会抛出以下错误:“无法将 xml 数据类型选择为 DISTINCT,因为它不具有可比性。”

让我知道我在这里缺少什么?

In my code I have the following Linq Query:

IQueryable<Data> charts = (from report in ctx.Charts group report by new 
    { 
        Name = report.ChartTitle.ChartType.ChartCategory.CategoryName,
        id = report.ChartTitle.ChartType.ChartCategory.ChartCategoryId, 
        Period = report.Period 
    } into d
    select new Data
    {
        Name = d.Key.Name,
        Id = d.Key.id,
        Period = d.Key.Period,            
        Reports = from r in d group r by new 
        { Title = r.ChartTitle.Name, id = r.ChartTitle.ChartTitleId } into rs
        select new Report
        {
            Title = rs.Key.Title,
            Id = rs.Key.id,
            Charts = (from c in rs group c by new 
                    { 
                        ChartId = c.ChartId, 
                        FiscalYear = c.FiscalYear, 
                        ModifiedDate = c.ChartView.ModifiedDate, 
                        Function = c.Function.DisplayName, 
                        ChartData=c.ChartView.ViewData
                    } into cs
                    select new ChartInfo 
                    { 
                        ChartId = cs.Key.ChartId, 
                        FiscalYear = cs.Key.FiscalYear,
                        ModifiedDate = cs.Key.ModifiedDate, 
                        Function = cs.Key.Function, 
                        ChartData=cs.Key.ChartData 
                    })
    }});

In the above code if I exclude the "ChartData" field (which is of XML datatype), the query executes fine. But when ever I include this field it throws the following error :"The xml data type cannot be selected as DISTINCT because it is not comparable."

Let me know what I am missing here?

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

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

发布评论

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

评论(2

长亭外,古道边 2024-12-12 07:48:06

您无法按 XML 类型进行分组。这是 SQL 限制,而不是 LINQ-to-SQL 限制。 (请参阅 使用 LINQ 对 XML 列字段进行分组 和 < a href="https://stackoverflow.com/questions/5222212/select-an-xml-type-column-in-select-query-with-group-by-sql-server-2008">选择一个通过 SQL Server 2008 分组的选择查询中的 xml 类型列)

需要按 XML 列分组吗?另一种方法是按其他列进行分组,然后选择第一个 XML 值作为结果。

Charts = (from c in rs group c by new  
{
    ChartId = c.ChartId,
    FiscalYear = c.FiscalYear,
    ModifiedDate = c.ChartView.ModifiedDate,
    Function = c.Function.DisplayName,
} into cs                     
select new ChartInfo
{
    ChartId = cs.Key.ChartId,
    FiscalYear = cs.Key.FiscalYear,
    ModifiedDate = cs.Key.ModifiedDate,
    Function = cs.Key.Function,
    ChartData=cs.Value.FirstOrDefault().ChartData
}) 

使用 LINQ-to-SQL 时,分组的项目仍然可以访问 - 您不需要像在 SQL 中那样在 group by 子句中包含每个“选定”属性/列。

You can't group by XML types. This is a SQL restriction, not a LINQ-to-SQL retriction. (See Group by on XML column field with LINQ and select an xml type column in select query with group by SQL Server 2008)

Do you need to group by the XML column? The alternative would be to group by your other columns and then select the first XML value as a result.

Charts = (from c in rs group c by new  
{
    ChartId = c.ChartId,
    FiscalYear = c.FiscalYear,
    ModifiedDate = c.ChartView.ModifiedDate,
    Function = c.Function.DisplayName,
} into cs                     
select new ChartInfo
{
    ChartId = cs.Key.ChartId,
    FiscalYear = cs.Key.FiscalYear,
    ModifiedDate = cs.Key.ModifiedDate,
    Function = cs.Key.Function,
    ChartData=cs.Value.FirstOrDefault().ChartData
}) 

When using LINQ-to-SQL the items being grouped are still accessible - you don't need to include every 'selected' property / column in the group by` clause like you would in SQL.

很快妥协 2024-12-12 07:48:06

您没有告诉我们 ChartData 的实际数据类型是什么,但从您描述的错误来看,问题似乎在于,无论该数据类型是什么,它都没有实现必需的 IComparable 接口接口(如果您希望数据类型的实例具有可比性)

You did not tell us what is the actual datatype of the ChartData, but from the error you are describing it looks like the problem is that whatever this datatype is it does not implement the IComparable interface which is a required interface if you want instances of the datatype to be comparable

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