2

Can anybody help me get this work?

I have the following code :

dt.Columns.Add("Field1", typeof(String));
dt.Columns.Add("Field2", typeof(String));
dt.Columns.Add("NR", typeof(Double));

dt.Rows.Add("A", "990001", 100);
dt.Rows.Add("A", "990001", 120);
dt.Rows.Add("B", "990001", 200);
dt.Rows.Add("B", "990001", 300);

var fields = new List<string>() { "Field1", "Field2" };

var q = dt.AsEnumerable().AsQueryable()
          .GroupBy("new(" +  qfields + ")", "it")
          .Select("new(Key as Group, Sum(NR) as Tot)");

When I execute this code I get the error:

No property or field 'NR' exists in type 'DataRow'

I need the result as:

A, 990001, 220
B, 990001, 500
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Shalinn79
  • 23
  • 2

1 Answers1

1
var q =
    dt.AsEnumerable()
        .AsQueryable()
        .GroupBy("new(it[\"Field1\"] as Field1,it[\"Field2\"]as Field2)", "it")
        .Select("new(Key as Group, Sum(double(it[\"NR\"])) as Tot)");
foreach (var v in q)
{
    Console.WriteLine(v);
}
Vladimir
  • 2,082
  • 1
  • 13
  • 27