Note that there are some explanatory texts on larger screens.

plurals
  1. PODataTable group the result in one row
    primarykey
    data
    text
    <p>I have a DataTable and want to group Name, LastName and Comment. The rest should be in the same row. In my Code firstly i make ID's values as header and then organize the Attribute values to each ID. What I want here is to group the the same Name, Lastname and Comment with their ID values. My first Table looks like that:</p> <pre><code>ID Name Lastmame Comment Attribute 1 kiki ha hello FF 3 lola mi hi AA 2 ka xe what UU 2 kiki ha hello SS </code></pre> <p>After I use my code:</p> <pre><code> Name Lastname Comment 1 3 2 kiki ha hello FF lola mi hi AA ka xe what UU kiki ha hello SS </code></pre> <p>What I want to have is:</p> <pre><code> Name Lastname Comment 1 3 2 kiki ha hello FF SS lola mi hi AA ka xe what UU </code></pre> <p>My Code:</p> <pre><code>DataTable table1 = new DataTable("Kunde"); table1.Columns.Add("Comment", typeof(String)); table1.Columns.Add("Name", typeof(String)); table1.Columns.Add("Lastname", typeof(String)); DataTable comment = new DataTable("Comment"); comment.Columns.Add("ID", typeof(String)); comment.Columns.Add("Comment", typeof(String)); comment.Columns.Add("Attribute", typeof(String)); DataSet ds = new DataSet("DataSet"); ds.Tables.Add(table1); ds.Tables.Add(comment); object[] o1 = { "hello", "kiki", "ha" }; object[] o2 = { "hi", "lola", "mi" }; object[] o3 = { "what", "ka", "xe" }; object[] c1 = { 1, "hello", "FF" }; object[] c2 = { 3, "hi", "AA" }; object[] c3 = { 2, "what", "UU" }; object[] c4 = { 2, "hello", "SS" }; table1.Rows.Add(o1); table1.Rows.Add(o2); table1.Rows.Add(o3); comment.Rows.Add(c1); comment.Rows.Add(c2); comment.Rows.Add(c3); comment.Rows.Add(c4); var results = from tb1 in comment.AsEnumerable() join tb2 in table1.AsEnumerable() on tb1.Field&lt;string&gt;("Comment") equals tb2.Field&lt;string&gt;("Comment") select new { ID = tb1.Field&lt;String&gt;("ID"), Name = tb2.Field&lt;String&gt;("Name"), Lastname = tb2.Field&lt;String&gt;("Lastname"), Comment = tb1.Field&lt;String&gt;("Comment"), Attribute = tb1.Field&lt;String&gt;("Attribute"), }; DataTable result = LINQToDataTable(results); var products = result.AsEnumerable() .GroupBy(c =&gt; c["ID"]) .Where(g =&gt; !(g.Key is DBNull)) .Select(g =&gt; (string)g.Key) .ToList(); var newtable = result.Copy(); products.ForEach(p =&gt; newtable.Columns.Add(p, typeof(string))); foreach (var row in newtable.AsEnumerable()) { if (!(row["ID"] is DBNull)) row[(string)row["ID"]] = row["Attribute"]; } newtable.Columns.Remove("ID"); newtable.Columns.Remove("Attribute"); var result11 = from t1 in newtable.AsEnumerable() group t1 by new { Name = t1.Field&lt;String&gt;("Name"), LastName = t1.Field&lt;String&gt;("LastName"), Comment = t1.Field&lt;String&gt;("Comment"), } into grp select new { Name = grp.Key.Name, LastName = grp.Key.LastName, Comment = grp.Key.Comment, //Something here }; </code></pre> <p><code>LINQToDataTable</code> method definition</p> <pre><code>using System.Reflection; public DataTable LINQToDataTable&lt;T&gt;(IEnumerable&lt;T&gt; varlist) { DataTable dtReturn = new DataTable(); // column names PropertyInfo[] oProps = null; if (varlist == null) return dtReturn; foreach (T rec in varlist) { if (oProps == null) { oProps = ((Type)rec.GetType()).GetProperties(); foreach (PropertyInfo pi in oProps) { Type colType = pi.PropertyType; if ((colType.IsGenericType) &amp;&amp; (colType.GetGenericTypeDefinition() == typeof(Nullable&lt;&gt;))) { colType = colType.GetGenericArguments()[0]; } dtReturn.Columns.Add(new DataColumn(pi.Name, colType)); } } DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps) { dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue (rec, null); } dtReturn.Rows.Add(dr); } return dtReturn; } </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.
 

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