In a dynamic linq query, how do you refer to a field?

c# dynamic-linq

Question

I have a query that must be dynamic; I believe the syntax is OK for the most part, but I am unsure of how to name the column I want to group by. No matter what method I use, I always get the error message "No property or field 'name' exists in type 'datarow'."

IQueryable<Result> aggregate1 = 
      query1Data.Tables[0].AsEnumerable().AsQueryable()
                .GroupBy("name", "it")
                .Select<Result>("new(Key as Group, Sum(value)as Total)");

I believe I must use anything similarField<string>("name") But I'm at a loss for words. I've seen the issue on a few places, but no one appears to have a definitive solution on how to solve it.

1
0
8/12/2010 9:31:53 AM

Popular Answer

I'm not sure I understand what you want precisely, but here is a little piece of code you may use as an example:

var dataTable = new DataTable();
dataTable.Columns.Add("Name", typeof(String));
dataTable.Columns.Add("Value", typeof(Int32));
dataTable.Rows.Add("A", 1);
dataTable.Rows.Add("A", 2);
dataTable.Rows.Add("B", 3);
dataTable.Rows.Add("B", 4);

var aggregate = dataTable.AsEnumerable()
  .GroupBy(row => row.Field<String>("Name"), row => dataRow.Field<Int32>("Value"))
  .Select(group => new { Name = group.Key, Total = group.Sum() });

foreach (var row in aggregate)
  Console.WriteLine(row.Name + " = " + row.Total);
0
8/12/2010 9:55:32 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow