Group by on XML column field with LINQ

2019-07-16 08:36发布

问题:

Is it possible to group by on field which contains XML data with LINQ?

I am getting The XML data type cannot be compared or sorted, except when using the IS NULL operator.error.

回答1:

No, it's not possible - you can't sort or group on a field if you can't compare the field values for equality / less than / greater than operations. This isn't a LINQ to SQL restriction, it's SQL Server 2005 that doesn't support this.

If you are trying to de-dupe rows based on having the same literal XML values, you could add a new column that is a copy of the XML column converted to varchar instead, then you can group on that column's value.

Generally though, grouping on XML is a bit of a smell and I would try to use a alternative group key if at all possible.



标签: linq-to-sql