LINQ的不同工作不正常 [英] Linq distinct not working correctly

查看:162
本文介绍了LINQ的不同工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ查询一个奇怪的问题。我使用LINQPad 4做出一些使用使用LinqToSQL作为LinqPad司机正则表达式查询



下面是我试图使查询:

 (从比赛中从s在SystemErrors 

选择Regex.Match(s.Description,.. )
选择新的
{
FamilyCode = match.Groups [FamilyCode]。价值
ProductPrefix = match.Groups [ProductPrefix]。价值
BillingGroup = match.Groups [BillingGroup]。价值
债务人= match.Groups [债务人。价值
})。鲜明的()

正如你可以看到我试图使用组日志表的文本说明提取数据。查询工作,但分明不想工作,这将返回所有匹配行。



我已阅读,不同的应与匿名类型的工作,每一个匹配属性。更为奇怪的是,不同的不实际做的东西,它由FamilyCode订单按字母顺序排列的值(然后ProductPrefix,等等)。



有一个人,为什么一个想法这是不工作?
谢谢



下面是显示在LinqPad的SQL标签:

  DECLARE @ P0的NVarChar(1000)=大正则表达式'
DECLARE @ P1的NVarChar(1000)='FamilyCode'
DECLARE @ P2的NVarChar(1000)='ProductPrefix'
DECLARE @ P3的NVarChar(1000)='BillingGroup'
DECLARE @ P4的NVarChar(1000)='债务人'

SELECT DISTINCT [T2]。[说明] AS [输入] [T2] [价值],[T2] [值2],[T2]。[VALUE3],[T2]。[VALUE4],[T2] [值5]
FROM(
选择[T1]。[说明],[T1] [价值],@ P1 AS [值],@ p2的AS [VALUE3] // @ P3 AS [VALUE4],@ P4 AS [值5]
FROM(
选择[T0]。[说明],@ P0 AS [值]
FROM [关于SystemError] AS [T0]
)AS [T1]
)AS [T2]


解决方案

  VAR的结果=从eachError在SystemErrors 
让利匹配= Regex.Match(eachError.Description,...)
组eachError通过新的
{
FamilyCode = match.Groups [FamilyCode ] .value的,
ProductPrefix = match.Groups [ProductPrefix]。价值
BillingGroup = match.Groups [BillingGroup]。价值
债务人= match.Groups [债务人] .value的
}
到独特
选择unique.key;

当您使用鲜明的(),这是通过指针每个对象不同,没有价值,因为选择新的{} 是对象类型不是值类型。尝试使用GROUP BY来代替。


I'm having a strange problem with a linq query. I'm using LINQPad 4 to make some a query that uses regular expression using LinqToSQL as the LinqPad driver.

Here's the query that I'm trying to make :

(from match in
from s in SystemErrors
select Regex.Match(s.Description, "...")
select new 
{
  FamilyCode = match.Groups["FamilyCode"].Value,
  ProductPrefix = match.Groups["ProductPrefix"].Value,
  BillingGroup = match.Groups["BillingGroup"].Value,
  Debtor = match.Groups["Debtor"].Value
}).Distinct()

As you can see I'm trying to extract data from a text description in a log table using groups. The query works, but the Distinct doesn't want to work, it returns a line for all Match.

I have read that distinct should work with anonymous type, matching each property. Even more strange is that distinct does actually do something, it orders the values alphabetically by the FamilyCode (and then by ProductPrefix, etc.).

Has someone an idea on why this isn't working? Thanks

Here is what is displayed in the SQL tab of LinqPad :

DECLARE @p0 NVarChar(1000) = 'Big Regexp'
DECLARE @p1 NVarChar(1000) = 'FamilyCode'
DECLARE @p2 NVarChar(1000) = 'ProductPrefix'
DECLARE @p3 NVarChar(1000) = 'BillingGroup'
DECLARE @p4 NVarChar(1000) = 'Debtor'

SELECT DISTINCT [t2].[Description] AS [input], [t2].[value], [t2].[value2], [t2].[value3], [t2].[value4], [t2].[value5]
FROM (
    SELECT [t1].[Description], [t1].[value], @p1 AS [value2], @p2 AS [value3], @p3 AS [value4], @p4 AS [value5]
    FROM (
        SELECT [t0].[Description], @p0 AS [value]
        FROM [SystemError] AS [t0]
        ) AS [t1]
    ) AS [t2]

解决方案

var result = from eachError in SystemErrors
             let match = Regex.Match(eachError.Description, "...")
             group eachError by new 
             {
              FamilyCode = match.Groups["FamilyCode"].Value,
              ProductPrefix = match.Groups["ProductPrefix"].Value,
              BillingGroup = match.Groups["BillingGroup"].Value,
              Debtor = match.Groups["Debtor"].Value
             }
             into unique
             select unique.key;

When you use Distinct(), it's distinct by pointer to each object, not value because select new {} is object type not value type. Try using group by instead.

这篇关于LINQ的不同工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆