0

I want the data of this statement into dictionary/datatable.

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

Please help, how can i convert the distinctValues in datatable?

Update:

If anybody know a better way to find distinct values, then please suggest. I want to pass this in a c# functions and i cannot pass them as var.

4
  • Why cant you use var? It should be cast as IList<Entity> according to your sample? All your method signature would have to look like is Method(IList<Entity> distinctValues){}... Commented May 9, 2013 at 14:26
  • 1
    @nakchak The result will be a List<{anonymous type}>. Entity is a property, not a class. Commented May 9, 2013 at 14:29
  • good point, that said why bother with the anon class at all if all it has is a single string property? Commented May 9, 2013 at 14:33
  • Everything is so complicated. Can't anybody suggest a simple and easy way to find distinct values and store them in a datatable. Commented May 10, 2013 at 4:34

3 Answers 3

2

Check the CopyToDataTable() extension method.

Edit: Changed to match Servy's comment.

Sign up to request clarification or add additional context in comments.

1 Comment

That only works for sequences of data rows. This other CopyToDataTable method is what's needed to create one from any arbitrary sequence. Either that or you'll need to modify the query such that it keeps the data rows.
1

What you want is to be able to get the distinct items from a sequence, based on a selector, but keeping the items from the original sequence rather than the results of the selector. This is commonly named DistinctBy. MoreLinq has an implementation, which (with minor modifications) is:

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

Using that, along with the CopyToDataTable method to convert the rows back into a table, you can now do:

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

2 Comments

Getting this error: get this error: 'System.Data.EnumerableRowCollection<System.Data.DataRow>' does not contain a definition for 'DistinctBy' and no extension method 'DistinctBy' accepting a first argument of type 'System.Data.EnumerableRowCollection<System.Data.DataRow>' could be found (are you missing a using directive or an assembly reference?) What should i need to add more?
@user1254053 You need the function that I provided in this answer; it is the implementation of DistinctBy.
0

Try an extention method like this, maybe it's not so fast, but it's really simple:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.