本文介绍了你可以创建一个简单的“EqualityComparer&LT; T&GT;'使用兰巴表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 重要:这是不会一个LINQ到SQL 问题。这是LINQ的对象。IMPORTANT : THIS IS NOT A LINQ-TO-SQL QUESTION. This is LINQ to objects. 短的问题: 有一个简单的。这样在LINQ to对象获得基于对对象的关键属性列表对象的独特列表Is there a simple way in LINQ to objects to get a distinct list of objects from a list based on a key property on the objects. 长的问题: 我试图做一个 鲜明的()对象有一个关键的作为其特性之一的名单> 操作。I am trying to do a Distinct() operation on a list of objects that have a key as one of their properties.class GalleryImage { public int Key { get;set; } public string Caption { get;set; } public string Filename { get; set; } public string[] Tags {g et; set; }} 我画廊名单对象包含 GalleryImage [] 。由于方式的web服务工作[原文]我有 GalleryImage 对象的副本。我认为这将是一个简单的事情,使用鲜明的()来得到清晰的列表。Because of the way the webservice works [sic] I have duplicates of the GalleryImage object. i thought it would be a simple matter to use Distinct() to get a distinct list.这是LINQ查询我想用:This is the LINQ query I want to use :var allImages = Galleries.SelectMany(x => x.Images);var distinctImages = allImages.Distinct<GalleryImage>(new EqualityComparer<GalleryImage>((a, b) => a.id == b.id)); 问题是, EqualityComparer 是一个抽象一流我不想为: 在 GalleryImage ,因为它会产生 必须写一个单独的类来实现的IEqualityComparer 为这里显示 implement IEquatable on GalleryImage because it is generatedhave to write a separate class to implement IEqualityComparer as shown here有一个具体的实现,我很想念 EqualityComparer 地方呢?Is there a concrete implementation of EqualityComparer somewhere that I'm missing?我本来以为会有一个简单的方法。从基于一键一组获得独立的对象I would have thought there would be an easy way to get 'distinct' objects from a set based on a key.推荐答案(这里有两个解决方案 - 请参阅结束第二个):(There are two solutions here - see the end for the second one): MiscUtil 库有 ProjectionEqualityComparer 类(和两个支持类利用类型推断的)My MiscUtil library has a ProjectionEqualityComparer class (and two supporting classes to make use of type inference).下面是使用它的一个例子: Here's an example of using it:EqualityComparer<GalleryImage> comparer = ProjectionEqualityComparer<GalleryImage>.Create(x => x.id); 下面的代码(注释去掉)Here's the code (comments removed)// Helper class for constructionpublic static class ProjectionEqualityComparer{ public static ProjectionEqualityComparer<TSource, TKey> Create<TSource, TKey>(Func<TSource, TKey> projection) { return new ProjectionEqualityComparer<TSource, TKey>(projection); } public static ProjectionEqualityComparer<TSource, TKey> Create<TSource, TKey> (TSource ignored, Func<TSource, TKey> projection) { return new ProjectionEqualityComparer<TSource, TKey>(projection); }}public static class ProjectionEqualityComparer<TSource>{ public static ProjectionEqualityComparer<TSource, TKey> Create<TKey>(Func<TSource, TKey> projection) { return new ProjectionEqualityComparer<TSource, TKey>(projection); }}public class ProjectionEqualityComparer<TSource, TKey> : IEqualityComparer<TSource>{ readonly Func<TSource, TKey> projection; readonly IEqualityComparer<TKey> comparer; public ProjectionEqualityComparer(Func<TSource, TKey> projection) : this(projection, null) { } public ProjectionEqualityComparer( Func<TSource, TKey> projection, IEqualityComparer<TKey> comparer) { projection.ThrowIfNull("projection"); this.comparer = comparer ?? EqualityComparer<TKey>.Default; this.projection = projection; } public bool Equals(TSource x, TSource y) { if (x == null && y == null) { return true; } if (x == null || y == null) { return false; } return comparer.Equals(projection(x), projection(y)); } public int GetHashCode(TSource obj) { if (obj == null) { throw new ArgumentNullException("obj"); } return comparer.GetHashCode(projection(obj)); }} 第二类解决方案 要只为鲜明做到这一点,您可以使用的 DistinctBy 扩展在 MoreLINQ :To do this just for Distinct, you can use the DistinctBy extension in MoreLINQ: public static IEnumerable<TSource> DistinctBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { return source.DistinctBy(keySelector, null); } public static IEnumerable<TSource> DistinctBy<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) { source.ThrowIfNull("source"); keySelector.ThrowIfNull("keySelector"); return DistinctByImpl(source, keySelector, comparer); } private static IEnumerable<TSource> DistinctByImpl<TSource, TKey> (IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) { HashSet<TKey> knownKeys = new HashSet<TKey>(comparer); foreach (TSource element in source) { if (knownKeys.Add(keySelector(element))) { yield return element; } } }在这两种情况下, ThrowIfNull 是这样的:In both cases, ThrowIfNull looks like this:public static void ThrowIfNull<T>(this T data, string name) where T : class{ if (data == null) { throw new ArgumentNullException(name); }} 这篇关于你可以创建一个简单的“EqualityComparer&LT; T&GT;'使用兰巴表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 01:00