C# 根据你们的样品,你们的价格是多少?您的方法签名必须看起来像是method(IList distinctValues){}…@nakchak。结果将是一个列表实体是一个属性,而不是一个类。很好的一点是,如果anon类只有一个字符串属性,为什么还要麻烦它呢?一
C# 根据你们的样品,你们的价格是多少?您的方法签名必须看起来像是method(IList distinctValues){}…@nakchak。结果将是一个列表实体是一个属性,而不是一个类。很好的一点是,如果anon类只有一个字符串属性,为什么还要麻烦它呢?一,c#,linq,C#,Linq,根据你们的样品,你们的价格是多少?您的方法签名必须看起来像是method(IList distinctValues){}…@nakchak。结果将是一个列表实体是一个属性,而不是一个类。很好的一点是,如果anon类只有一个字符串属性,为什么还要麻烦它呢?一切都太复杂了。任何人都不能建议一种简单易行的方法来查找不同的值并将其存储在datatable中。获取此错误:获取此错误:“System.Data.EnumerableRowCollection”不包含“DistinctBy”的定义,并且找不到接
根据你们的样品,你们的价格是多少?您的方法签名必须看起来像是method(IList distinctValues){}…@nakchak。结果将是一个
列表
<代码>实体是一个属性,而不是一个类。很好的一点是,如果anon类只有一个字符串属性,为什么还要麻烦它呢?一切都太复杂了。任何人都不能建议一种简单易行的方法来查找不同的值并将其存储在datatable中。获取此错误:获取此错误:“System.Data.EnumerableRowCollection”不包含“DistinctBy”的定义,并且找不到接受“System.Data.EnumerableRowCollection”类型的第一个参数的扩展方法“DistinctBy”(是否缺少using指令或程序集引用?)我还需要添加什么?@user1254053您需要我在回答中提供的函数;它是DistinctBy
的实现。获取此错误:获取此错误:“System.Data.EnumerablerRowCollection”不包含“DistinctBy”的定义,也没有扩展方法可以找到接受类型为“System.Data.EnumerablerRowCollection”的第一个参数的od“DistinctBy”(是否缺少using指令或程序集引用?)我还需要添加什么?@user1254053您需要我在这个答案中提供的功能;它是DistinctBy
的实现。
var distinctValues = datatable.AsEnumerable()
.Select(row => new
{
Employee = row.Field<string>("Employee")
})
.Distinct()
.ToList();
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)
{
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;
}
}
}
var distinctTable = datatable.AsEnumerable()
.DistinctBy(row => row.Field<string>("Employee"))
.CopyToDataTable();
public DataTable ToDataTable<T>(this List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}