0

i am writing the following linq code in c#

      var groupedData = from b in dt.AsEnumerable()
                      group b by b.Field<string>("TypeColor") into g

                      select new
                      {

                          Key = g.Key,

                          Value = g.Select(r => r.Field<int>("Price")),
                          Price = g.Select(r => r.Field<int>("Price"))

                      };

 foreach (var g in groupedData)
    {
        string s = g.Key;
}

how can i select two or more field from the datatable and group the table with one field. Can any body help me? Here dt is a datatable which is binding from database

4

1 回答 1

0

如果您对行进行了分组,则需要聚合字段(类似于 SQL):

var groupedData = from b in dt.AsEnumerable()
                  group b by b.Field<string>("TypeColor") into g
                  select new
                  {
                      Color = g.Key,
                      PriceSum = g.Sum(r => r.Field<int>("Price")),
                      FirstValue = g.First().Field<int>("Value")
                  };

foreach (var g in groupedData)
{
    string color = g.Color;
    Double priceSum = g.PriceSum;
    int firstValue = g.FirstValue;
} 
于 2012-04-18T10:05:31.367 回答