设置唯一值以查找编辑数据源

时间:2013-06-12 15:00:41

标签: c# .net winforms devexpress

我正在尝试使用下面的代码将非重复值放入查找编辑中,但变量unique始终为null,我不知道问题出在哪里。

请帮忙吗?

List<VueItemItemUnit> liste = ObjReservation.LoadAllFamilles();
var unique =
      from element in liste
      group element by element.FA_CODE into Group
      where Group.Count() == 1
      select Group.Key;

lookUpFamille.Properties.DataSource = unique;

2 个答案:

答案 0 :(得分:0)

试试这个:

var unique = liste.Distinct(element => element.FA_CODE).Select(element => element.FA_CODE);

答案 1 :(得分:0)

我建议你使用以下方法:

lookUpFamille.Properties.DataSource = list.DistinctBy(e => e.FA_CODE).ToList();
//...
// DistinctBy<T,TKey> extension
static class EnumerableHelper {
    public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector) {
        return source.Distinct(new EqualityComparer<T, TKey>(keySelector));
    }
    class EqualityComparer<T, TKey> : IEqualityComparer<T> {
        readonly Func<T, TKey> keySelector;
        public EqualityComparer(Func<T, TKey> keySelector) {
            this.keySelector = keySelector;
        }
        bool IEqualityComparer<T>.Equals(T x, T y) {
            return Equals(keySelector(x), keySelector(y));
        }
        int IEqualityComparer<T>.GetHashCode(T obj) {
            return keySelector(obj).GetHashCode();
        }
    }
}