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 不想工作,它为所有匹配返回一行。
我读过,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
Distinct()
时,它通过指向每个对象的指针而不是值来区分,因为select new {}
是对象类型而不是值类型。尝试使用 group by 代替。When you use
Distinct()
, it's distinct by pointer to each object, not value becauseselect new {}
is object type not value type. Try using group by instead.另一方面,您可以使用
.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.