匿名类型的 IEqualityComparer
我有这个,
var n = ItemList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList();
n.AddRange(OtherList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList(););
如果允许
n = n.Distinct((x, y) => x.Vchr == y.Vchr)).ToList();
我尝试使用通用 LambdaComparer 但由于我使用匿名类型,因此没有与之关联的类型。
“帮助我,欧比万·克诺比,你是我唯一的希望”
I have this
var n = ItemList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList();
n.AddRange(OtherList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList(););
I would like to do this if it where allowed
n = n.Distinct((x, y) => x.Vchr == y.Vchr)).ToList();
I tried using the generic LambdaComparer but since im using anonymous types there is no type associate it with.
"Help me Obi Wan Kenobi, you're my only hope"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
诀窍是创建一个仅适用于推断类型的比较器。 例如:
现在我可以执行以下操作...hacky解决方案:
The trick is to create a comparer that only works on inferred types. For instance:
Now I can do the following ... hacky solution:
大多数时候,当您进行比较(相等或排序)时,您感兴趣的是选择要比较的键,而不是相等或比较方法本身(这是 Python 列表排序 API 背后的想法)。
这里有一个示例键相等比较器。
Most of the time when you compare (for equality or sorting) you're interested in choosing the keys to compare by, not the equality or comparison method itself (this is the idea behind Python's list sort API).
There's an example key equality comparer here.
我注意到 JaredPar 的答案并没有完全回答问题,因为像 Distinct 和 except 这样的 set 方法需要
IEqualityComparer
而不是IComparer
。 下面假设 IEquatable 将有一个合适的 GetHashCode,并且它当然有一个合适的 Equals 方法。JaredPar 的答案中使用了与静态类技巧相同的推论。
更一般地说,您可以提供两个
Func
:一个Func
用于检查相等性和Func< /code> 选择哈希码。
I note that JaredPar's answer does not quite answer the question since the set methods like Distinct and Except require an
IEqualityComparer<T>
not anIComparer<T>
. The following assumes that an IEquatable will have a suitable GetHashCode, and it certainly has a suitable Equals method.Where the same inference from a static class trick is used as in JaredPar's answer.
To be more general you could provide two
Func
s: aFunc<T, T, bool>
to check equality andFunc<T, T, int>
to select a hash code.