using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WooDev.Common.Extend
{
public static class LinqExtensions
{
public static IEnumerable<T> Distinct<T>(
this IEnumerable<T> source, Func<T, T, bool> comparer)
where T : class
=> source.Distinct(new DynamicEqualityComparer<T>(comparer));
private sealed class DynamicEqualityComparer<T> : IEqualityComparer<T>
where T : class
{
private readonly Func<T, T, bool> _func;
public DynamicEqualityComparer(Func<T, T, bool> func)
{
_func = func;
}
public bool Equals(T x, T y) => _func(x, y);
public int GetHashCode(T obj) => 0;
}
}
}
扩展代码如上:
使用方式:指定属性去重
Distinct((a,b)=>a.Id==b.Id)
我的完整代码如下:
var listdata = DbClient.Queryable<DEV_FLOW_INST_OPTION>()
.LeftJoin<DEV_FLOW_INST_NODE>((o, i) => o.NODE_STR_ID == i.NODE_STRID)
.Where(o => o.INST_ID == appInst.ID)
.Select((o, i) =>
new OptionView
{
Id = o.ID,
NodeName = i.TEXT_VALUE,
CreateDatetime = o.CREATE_TIME,
CreateUserId = o.CREATE_USERID,
Opinion = o.APP_OPTION,
}
).ToList().Distinct((a,b)=>a.Id==b.Id).ToList();