Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Essentially, you want to retrieve specific fields from a DataTable without hardcoding the field names. </p> <p>The following code will return a single dictionary object per row with the fields you specify in your array. There is no need to create additional extension methods or comparers:</p> <pre><code>var result = (from row in _dataTable.AsEnumerable() let projection = from fieldName in fields select new {Name = fieldName, Value = row[fieldName]} select projection.ToDictionary(p=&gt;p.Name,p=&gt;p.Value)); </code></pre> <p>The inner select picks the field values you need from each table row and stores them in the projection variable. The outer select converts this variable in a Dictionary</p> <p>You can iterate over the result to get specific fields like this:</p> <pre><code>foreach (var row in result) { Console.WriteLine(row["field1"]); } </code></pre> <p><strong>EDIT:</strong> The above code doesn't return distinct values. It is possible to return distinct values without writing a special comparer using <em>group by</em> but the code is not very pretty:</p> <pre><code>var result = (from row in table.AsEnumerable() let projection = from fieldName in fields select new { Name = fieldName, Value = row[fieldName] } group projection by projection.Aggregate((v, p) =&gt; new { Name = v.Name + p.Name, Value = (object)String.Format("{0}{1}", v.Value, p.Value) }) into g select g.FirstOrDefault().ToDictionary(p=&gt;p.Name,p=&gt;p.Value)); </code></pre> <p>The Aggregate creates a new projection whose Name and Value properties are the concatenation of all name and value fields. The result of the aggregate is used to group all rows and return the first row of each group. It works but it is definitely ugly.</p> <p>It would be better to create a simple DictionaryComparer like the following code:</p> <pre><code> public class DictionaryComparer&lt;TKey,TValue&gt;: EqualityComparer&lt;Dictionary&lt;TKey,TValue&gt;&gt; { public override bool Equals(Dictionary&lt;TKey, TValue&gt; x, Dictionary&lt;TKey, TValue&gt; y) { //True if both sequences of KeyValuePair items are equal var sequenceEqual = x.SequenceEqual(y); return sequenceEqual; } public override int GetHashCode(Dictionary&lt;TKey, TValue&gt; obj) { //Quickly detect differences in size, defer to Equals for dictionaries //with matching sizes return obj.Count; } } </code></pre> <p>This allows you to write:</p> <pre><code> var result = (from row in table.AsEnumerable() let projection = from fieldName in fields select new {Name = fieldName, Value = row[fieldName]} select projection.ToDictionary(p=&gt;p.Name,p=&gt;p.Value)) .Distinct(new DictionaryComparer&lt;string, object&gt;()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload