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 に答える