Linq 不同的无法正常工作

发布于 2024-10-07 17:32:52 字数 1289 浏览 0 评论 0原文

我在 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 不想工作,它为所有匹配返回一行。

我读过,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]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗷兮 2024-10-14 17:32:52
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 代替。

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.

这个俗人 2024-10-14 17:32:52

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文