【问题标题】:EF: The text data type cannot be selected as DISTINCT because it is not comparableEF:文本数据类型不能选择为 DISTINCT,因为它不可比较
【发布时间】:2018-09-20 16:11:22
【问题描述】:

我收到此错误是因为我有一个包含“文本”类型列的 SQL Server 表。

The text data type cannot be selected as DISTINCT because it is not comparable

有什么方法可以在不改变数据类型的情况下解决这个问题?

这是我的 linq 语句(很长):

                var query = (from s in db.tblSuppliers
                             join p in
                                 (
                                    from p1 in db.tblSupplierPricingSchemes
                                    select new
                                    {
                                        p1.SupplierID,
                                        p1.PSLangPairID,
                                        p1.CustomerID,
                                        p1.PSLanguageStatus,
                                        p1.PSPriceBasis,
                                        p1.PSMinFlatCharge,
                                        p1.PSTrxPrf,
                                        p1.PSNoMatch,
                                        p1.PSFuzzy,
                                        p1.PS100Match_Rep,
                                        p1.PSTrxOnly,
                                        p1.PSPrfOnly,
                                        p1.PSLinquisticHourlyRate,
                                        p1.PSDTPType,
                                        p1.PSDTPRate,
                                        p1.PS_FZ50,
                                        p1.PS_FZ75,
                                        p1.PS_FZ85,
                                        p1.PS_FZ95,
                                        p1.PS_FZ100,
                                        p1.PS_FZREPS,
                                        p1.PSPerfectMatch
                                    }
                                 ) on s.SupplierID equals p.SupplierID
                             join p2 in
                                 (
                                        from p in db.tblSupplierPricingSchemes
                                        where custID.Contains(p.CustomerID) && p.PSLangPairID == languagePairID
                                        group p by new { p.SupplierID, p.PSLangPairID, p.PSPriceBasis } into g
                                        let CustomerID = g.Max(uh => uh.CustomerID)
                                        select new
                                        {
                                            g.Key.SupplierID,
                                            g.Key.PSLangPairID,
                                            g.Key.PSPriceBasis,
                                            CustomerID
                                        }
                                 ) on p.SupplierID equals p2.SupplierID
                             join b in db.tblPricingBasis on p.PSPriceBasis equals b.PricingBasisID
                             join ss in db.tblSupplierStatus on p.PSLanguageStatus equals ss.SupplierStatusID into g1
                             from ss in g1.DefaultIfEmpty()
                             join l in db.tblLangPairs on p.PSLangPairID equals l.ProductID
                             where l.ProductID == languagePairID
                                && p.PSLangPairID == p2.PSLangPairID
                                && p.CustomerID == p2.CustomerID
                                && p.PSPriceBasis == p2.PSPriceBasis
                             select new PreferredSupplier
                             {
                                 SupplierID = s.SupplierID,
                                 //SupplierName = s.CompanyName != null ? s.CompanyName + "-" + s.SupplierFirstName + " " + s.SupplierLastName
                                 //                                     : s.SupplierFirstName + " " + s.SupplierLastName,

                                 SupplierName = s.CompanyName != null
                                            ? s.SupplierFirstName != null || s.SupplierLastName != null
                                                    ? s.CompanyName + "-" + s.SupplierFirstName + " " + s.SupplierLastName
                                                    : s.CompanyName
                                            : s.SupplierFirstName + " " + s.SupplierLastName,

                                 CompanyName = s.CompanyName,
                                 SupplierFirstName = s.SupplierFirstName,
                                 SupplierLastName = s.SupplierLastName,

                                 SupplierStatus = p.CustomerID == customerID ? "Team Member" : ss.SupplierStatus,
                                 Email = (string)s.SupplierEmails,

                                 Rate = (s.VolumeDiscountType == 1 ? // Percentage
                                     //if the volume discount is as percentage then get the rate and multiple it by 1 - the discount percentage
                                        ((words > s.VolumeDiscountAmount && (task == "TM No Match" || task == "Translation/Proofreading")) ? 1 - s.VolumeDiscountValue : 1) *
                                                    (
                                                        rateField == "PSTrxPrf" ? p.PSTrxPrf :
                                                        rateField == "PSNoMatch" ? p.PSNoMatch :
                                                        rateField == "PSFuzzy" ? p.PSFuzzy :
                                                        rateField == "PS100Match_Rep" ? p.PS100Match_Rep :
                                                        rateField == "PSLinquisticHourlyRate" ? p.PSLinquisticHourlyRate :
                                                        rateField == "PSDTPRate" ? p.PSDTPRate :
                                                        rateField == "PS_FZ50" ? p.PS_FZ50 :
                                                        rateField == "PS_FZ75" ? p.PS_FZ75 :
                                                        rateField == "PS_FZ85" ? p.PS_FZ85 :
                                                        rateField == "PS_FZ95" ? p.PS_FZ95 :
                                                        rateField == "PS_FZ100" ? p.PS_FZ100 :
                                                        rateField == "PS_FZREPS" ? p.PS_FZREPS :
                                                        rateField == "PSPerfectMatch" ? p.PSPerfectMatch : null
                                                    ) :
                                     // Discount in Amount
                                     // Take the Rate and substract the amount to discount
                                                   (
                                                        rateField == "PSTrxPrf" ? p.PSTrxPrf :
                                                        rateField == "PSNoMatch" ? p.PSNoMatch :
                                                        rateField == "PSFuzzy" ? p.PSFuzzy :
                                                        rateField == "PS100Match_Rep" ? p.PS100Match_Rep :
                                                        rateField == "PSLinquisticHourlyRate" ? p.PSLinquisticHourlyRate :
                                                        rateField == "PSDTPRate" ? p.PSDTPRate :
                                                        rateField == "PS_FZ50" ? p.PS_FZ50 :
                                                        rateField == "PS_FZ75" ? p.PS_FZ75 :
                                                        rateField == "PS_FZ85" ? p.PS_FZ85 :
                                                        rateField == "PS_FZ95" ? p.PS_FZ95 :
                                                        rateField == "PS_FZ100" ? p.PS_FZ100 :
                                                        rateField == "PS_FZREPS" ? p.PS_FZREPS :
                                                        rateField == "PSPerfectMatch" ? p.PSPerfectMatch : null
                                                     ) - (s.VolumeDiscountValue == null ? 0 : s.VolumeDiscountValue)),
                                 //PSMinFlatCharge = p.PSMinFlatCharge,
                                MinimumFee = p.PSMinFlatCharge,

                                 //Basis = b.PricingBasisDesc,
                                 Basis = task == "DTP" || task == "DTP Edit" ? p.PSDTPType : b.PricingBasisDesc,

                                 StatusOrder = p.CustomerID == customerID ? 0 : p.PSLanguageStatus == null ? 1000 : p.PSLanguageStatus
                             }).Distinct();

【问题讨论】:

    标签: sql-server string linq-to-sql


    【解决方案1】:

    简单的答案是“不要使用文本”。

    几年前发布 SQL Server 2005 时,varchar(max) 已弃用它。

    您拥有的代码正在发出 SELECT DISTINCT。
    您需要修复模型/表,使其不是 text 数据类型

    【讨论】:

    • +1 好答案 gbn。你真的在这里存储了 8000 多个字符吗?如果是这样,请使用 varchar(max)。正如 gbn 所说,文本已经被弃用了一段时间。此外,我会质疑为什么这么多逻辑属于应用程序层。我建议将它的每一盎司推入一个存储过程。只是我的 2 美分。
    • 我希望有一种方法可以在不修改架构的情况下做到这一点,例如在 Linq 语句中进行转换。
    • @SteveB:你最终将不得不这样做。在 LINQ 中修复它不是正确的方法。
    • 我刚刚将我的列从 ntext 更改为 nvarchar(max) ,现在我可以使用 linq 获取详细信息
    • 同样的错误也会发生在 CLR 类型上,比如 geography、geometry 和 hierarchyid,它们绝对不会被弃用,并且解决问题的方法不是通过将所有内容转换为 varchar(MAX)/nvarchar(最大)。
    【解决方案2】:

    将任何文本数据类型转换为varchar(max)

    如果您能从代码中指出实际的列,我将向您展示select 的样子。

    【讨论】:

    • +1 - 如果您的问题出在存储过程中并且您无法更改列数据类型
    • 谢谢,这很好,因为我们的数据库希望我们删除并重新加载原始表!
    【解决方案3】:

    将您的 TEXT 数据类型转换为 VARCHAR(MAX),如下所示...

    SELECT DISTINCT CAST(emailaddr AS VARCHAR(MAX)) AS emailaddr FROM myTable
    

    或者使用 VARCHAR(8000),因为它实际上是相同的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多