0

I want to create a query that groups by 2 fields and does a count of those combined fields using Linq and C#. I then want to write the query to another DataTable. The current problem I am having is the Foreach Staement is returning an error about the input file being too large. I am not clear on what the .Key does. (I borrowed code from other posts.) I need both Type and Stage to be grouped together. What is the correct syntax that I need? Thank you.

      var GroupedDataQuery = from row in dTableFilteredCol1.AsEnumerable()
                               group row by row.Field<string>("Type")
                               into grp 
                               select new {
                                   Type = grp.Key,
                                   Stage = grp.Key,
                                   A_Count = grp.Count() 
                               };
        //
        DataTable dTable = new DataTable();
        foreach (var result in GroupedDataQuery)
        {
            dTable.Rows.Add(
            new object[] 
            {
             result.Type,
             result.Stage,
             result.A_Count
            });
        }
        return dTable;
    }
user977645
  • 1,077
  • 2
  • 7
  • 10

1 Answers1

1

If the Type and Stage are different fields of the source row, then you'll probably need something like

        var GroupedDataQuery = from row in dTableFilteredCol1.AsEnumerable()
                               let type = row.Field<string>("Type")
                               let stage = row.Field<string>("Stage")
                               group row by new { type, stage }
                               into grp
                                   select new
                                   {
                                       Type = grp.Key.type,
                                       Stage = grp.Key.stage,
                                       A_Count = grp.Count()
                                   };
penartur
  • 9,792
  • 5
  • 39
  • 50