将linq属性选择器添加到表达式

本文关键字:表达式 添加 选择器 linq 属性 | 更新日期: 2023-09-27 18:17:23

我在Linq中有以下表达式:

public static IEnumerable<T> NextDistinct<T>(this IEnumerable<T> items) 
{
    T previous = default(T);
    bool first = true;
    foreach(T item in items)
    {
        if (first || !Equals(previous, item)) 
        {
            first = false;
            previous = item;
            yield return item;
        }
    }
} 

我需要添加一个选择器,像这样:

.NextDistinct(i => i.articlepricehistory_sell)

我试过了,但是选择键不能正常工作:

public static IEnumerable<TSource> NextDistinct<TSource, TKey>(this IEnumerable<TSource> source,
            Func<TSource, TKey> keySelector)
        {
            TSource previous = default(TSource);
            bool first = true;
            foreach (TSource item in source)
            {
                if (first || !Equals(previous, item))
                {
                    first = false;
                    previous = item;
                    yield return item;
                }                
            }
        }

更新:

这是我的查询,我需要做一个独特的只使用articlepricehistory_sell列

var ArticlesSellHistory = dt.AsEnumerable()
select new
{
   articlepricehistory_sell = articlespricehistory.Field<Double>("articlepricehistory_sell"),
   articlepricehistory_date = articlespricehistory.Field<DateTime>("articlepricehistory_date")
})
.NextDistinct(i => i.articlepricehistory_sell)
.ToList();
结果:

365 05/09/2015 02:30:31 p.m.
370 11/10/2015 04:19:37 p.m.
369.59  11/10/2015 04:19:54 p.m.
365 11/10/2015 04:20:05 p.m.
365 11/10/2015 04:20:58 p.m.
365 11/10/2015 04:33:22 p.m.

预期结果:

365 05/09/2015 02:30:31 p.m.
370 11/10/2015 04:19:37 p.m.
369.59  11/10/2015 04:19:54 p.m.
365 11/10/2015 04:20:05 p.m.

将linq属性选择器添加到表达式

您所需要的就是将TSource previous替换为TKey previousKey,并将其与当前项键进行比较(两者都使用传递的选择器提取)。另外,允许为两个函数选择性地指定比较器也很好。所讨论的函数可以像这样

public static IEnumerable<TSource> NextDistinct<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    IEqualityComparer<TKey> keyComparer = null)
{
    if (source == null) throw new ArgumentNullException("source");
    if (keySelector == null) throw new ArgumentNullException("keySelector");
    if (keyComparer == null) keyComparer = EqualityComparer<TKey>.Default;
    var previousKey = default(TKey);
    bool first = true;
    foreach (TSource item in source)
    {
        var itemKey = keySelector(item);
        if (first || !keyComparer.Equals(previousKey, itemKey))
        {
            yield return item;
            first = false;
            previousKey = itemKey;
        }
    }
}