Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
601 views
in Technique[技术] by (71.8m points)

c# - Linq distinct not working correctly

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...