2013-05-09 49 views
0

我希望将此语句的数据放入dictionary/datatable。将Linq .ToList()放入数据表

  var distinctValues = datatable.AsEnumerable() 
       .Select(row => new 
       { 
        Employee = row.Field<string>("Employee") 
       }) 
       .Distinct() 
       .ToList(); 

请帮忙,我如何转换数据表中的distinctValues?

更新:

如果有人知道一个更好的方式来寻找不同的值,然后请建议。我想通过这个在c#函数中,我不能将它们作为var传递。

+0

为什么不能使用var?根据你的样本,它应该被视为IList ?您的所有方法签名必须看起来像Method(IList distinctValues){} ... – nakchak 2013-05-09 14:26:25

+1

@nakchak结果将是一个'List <{anonymous type}>'。 '实体'是_property_,而不是_class_。 – 2013-05-09 14:29:53

+0

好点,那就说为什么要烦扰一个匿名类,如果它只有一个字符串属性呢? – nakchak 2013-05-09 14:33:06

回答

2

检查CopyToDataTable()扩展方法。

编辑: 更改为符合Servy的评论。

+1

只适用于数据行的序列。 [此其他CopyToDataTable方法](http://msdn.microsoft.com/en-us/library/bb669096.aspx)是从任意序列创建一个所需的。无论是或者你将需要修改查询,以保持数据行。 – Servy 2013-05-09 14:18:26

1

你想要的是能够基于选择器从序列中获得不同的项目,但是保留项目与原始序列而不是选择器的结果。这通常被命名为DistinctByMoreLinq has an implementation,其中(稍作修改)是:

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; 
     } 
    } 
} 

利用这一点,与CopyToDataTable方法对行转换回表一起,你现在可以做的:

var distinctTable = datatable.AsEnumerable() 
    .DistinctBy(row => row.Field<string>("Employee")) 
    .CopyToDataTable(); 
+0

得到这个错误:得到这个错误:'System.Data.EnumerableRowCollection '不包含'DistinctBy'的定义,也没有接受'System.Data'类型的第一个参数的扩展方法'DistinctBy'。可以找到EnumerableRowCollection '(你是否缺少using指令或程序集引用?)我需要添加更多内容? – user1254053 2013-05-09 14:30:07

+0

@ user1254053您需要我在此答案中提供的功能;它是'DistinctBy'的实现。 – Servy 2013-05-09 14:30:53

+0

哇,很酷的答案。 – 2013-05-09 14:36:30

0

尝试一种推广方法像这样,也许它不是那么快,但它很简单:

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; 
     }