Linq distinct not working correctly

前端 未结 2 1001
一个人的身影
一个人的身影 2020-12-21 06:24

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\'

相关标签:
2条回答
  • 2020-12-21 06:55
    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.

    0 讨论(0)
  • 2020-12-21 07:00

    On the other hand, you can use .Distinct(IEqualityComparer<T>) overload and provided EqualityComparer for the object that you want to process.

    0 讨论(0)
提交回复
热议问题