Linq明显无法正常工作

我遇到了一个linq查询的奇怪问题。 我正在使用LINQPad 4进行一些使用LinqToSQL作为LinqPad驱动程序的正则表达式的查询。

这是我正在尝试的查询:

(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() 

正如您所看到的,我正在尝试使用组从日志表中的文本描述中提取数据。 查询有效,但Distinct不想工作,它返回所有Match的一行。

我已经读过,distinct应该使用匿名类型,匹配每个属性。 更奇怪的是,distinct确实做了一些事情,它通过FamilyCode(然后是ProductPrefix等)按字母顺序排序值。

有人知道为什么这不起作用? 谢谢

以下是LinqPad的SQL选项卡中显示的内容:

 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; 

当您使用Distinct() ,它与指向每个对象的指针不同,而不是值,因为select new {}是对象类型而不是值类型。 请尝试使用group by。

另一方面,您可以使用.Distinct(IEqualityComparer)重载并为要处理的对象提供EqualityComparer。