简体   繁体   English

无法将Distinct函数与C#和LINQ中的NTEXT字段一起使用

[英]Can't use Distinct function with an NTEXT field in C# and LINQ

I have a field called Description in a table that is of the type NTEXT. 我在NTEXT类型的表中有一个名为Description的字段。 My issue is that I need to use the method .Distinct() but am not able to as "The ntext data type cannot be selected as DISTINCT because it is not comparable." 我的问题是我需要使用方法.Distinct(),但不能使用“由于无法比较ntext数据类型不能选择为DISTINCT,”。

There is another question very similar to this found here but the accepted solution does not work for me. 这里还有另一个非常类似的问题但是被接受的解决方案对我不起作用。 When I try it I get a new error saying "Argument data type ntext is invalid for argument 1 of len function." 尝试时,出现新错误,提示“参数数据类型ntext对于len函数的参数1无效”。

Below is the Linq statement I had originally and the one below it is what I tried to do after reading the answer to the other similar question . 下面是我最初使用的Linq语句,下面是我阅读其他类似问题的答案后试图做的事情。

return (from vl in db.ValueLog
        join vc in db.ValueCodes on vl.ValueCode equals vc.ValueCode
        select new ValueLogItem
        {
            ValueId = vl.ValueId ,
            Description = vl.Description.Substring(0),
            Quantity = vl.Quantity,
            Code = vc.ValueCode
        }).Distinct().ToList();

Does anyone have any ideas on how to get around this restriction besides just converting the field? 除了转换字段外,还有人对如何解决此限制有任何想法吗? Thanks! 谢谢!

I am well aware that NTEXT, TEXT, and IMAGE have all been deprecated since 2005, but I am not able to change the field's type at the moment as I do not have access to do that. 我很清楚,自2005年以来,NTEXT,TEXT和IMAGE均已弃用,但由于无法访问,我目前无法更改该字段的类型。

I am also well aware that NTEXT, TEXT, and IMAGE can't be made DISTINCT. 我也很清楚,不能将NTEXT,TEXT和IMAGE设为DISTINCT。 I am trying to figure out a work around in LINQ. 我想弄清楚LINQ中的解决方法。 Doing it in SQL is extremely simple. 在SQL中执行此操作非常简单。

I am writing down the ways you can try to compare 我正在写下您可以尝试比较的方式

First Try GroupBy instead of Distinct. 首先尝试使用GroupBy代替Distinct。 GroupBY works in similar way GroupBY以类似的方式工作

Second Convert the Description into Byte array and compare the arrays. 其次,将描述转换为字节数组并比较数组。 For that, you can use 为此,您可以使用

static bool ByteArrayCompare(byte[] a1, byte[] a2) 
{
    return StructuralComparisons.StructuralEqualityComparer.Equals(a1, a2);
}

or 要么

return a1.SequenceEqual(b1);

or 要么

public bool Equality(byte[] a1, byte[] b1)
{
   int i;
   if (a1.Length == b1.Length)
   {
      i = 0;
      while (i < a1.Length && (a1[i]==b1[i])) //Earlier it was a1[i]!=b1[i]
      {
          i++;
      }
      if (i == a1.Length)
      {
          return true;
      }
   }

   return false;
}

The basic issue is that TEXT, NTEXT, and IMAGE data fields are not allowed in a SELECT using DISTINCT. 基本问题是使用DISTINCT的SELECT中不允许TEXT,NTEXT和IMAGE数据字段。 One of SQL Server's errors specifically states: SQL Server的错误之一特别指出:

Server: Msg 421, Level 16, State 1, Line 1
The text/ntext/image data type cannot be selected 
as DISTINCT because it is not comparable.

If you can't change the data type, you won't be able to use DISTINCT on that field. 如果您无法更改数据类型,则将无法在该字段上使用DISTINCT。 So, CAST/CONVERT the text field to VARCHAR(MAX) on the fly if you can't change the underlying data type. 因此,如果您不能更改基础数据类型,则将文本字段快速地CAST / CONVERT转换为VARCHAR(MAX)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM