如何使用LINQtoDataSet将dataTable转换为数组?
我有以下sql表架构:如何使用LINQtoDataSet将dataTable转换为数组?,linq,dataset,Linq,Dataset,我有以下sql表架构: procudtId productPrice Color ================================== 1 3 $ Blue 1 3 $ Red 1 3 $ Green 2 5 $ Blue 2 5 $ Red 我使用c代码将其输入数据集。 如
procudtId productPrice Color
==================================
1 3 $ Blue
1 3 $ Red
1 3 $ Green
2 5 $ Blue
2 5 $ Red
我使用c代码将其输入数据集。
如何使用LINQtoDataSet构建一个如下所示的数组
[ price:"3$", ColorList:<"Blue","Red","Green"> ;
price:"5$", ColorList:<"Blue","Red">]
谢谢我想我会分两步来做:
1. create the color lists
var dataRows = from row in ds.Tables[0].AsEnumerable()
//group row by row.Field<Int32>("TierId")
where row.Field<Int32>("ProductId") == 1
select
row.Field<String>("Color");
List<String> list = dataRows.ToList();
2. acquire the product price
3. combine them both to array
我想我将分两步来做:
1. create the color lists
var dataRows = from row in ds.Tables[0].AsEnumerable()
//group row by row.Field<Int32>("TierId")
where row.Field<Int32>("ProductId") == 1
select
row.Field<String>("Color");
List<String> list = dataRows.ToList();
2. acquire the product price
3. combine them both to array
我认为这会奏效:
//dt is the DataTable you're working with.
var groups = from row in dt.AsEnumerable()
group row["Color"] by row["productPrice"] into prices
select prices;
var query = from g in groups
select new
{
price = g.Key,
ColorList = g.ToList()
};
如果这样做不行,请告诉我,我会编辑。我认为这样做可以:
//dt is the DataTable you're working with.
var groups = from row in dt.AsEnumerable()
group row["Color"] by row["productPrice"] into prices
select prices;
var query = from g in groups
select new
{
price = g.Key,
ColorList = g.ToList()
};
如果不行,请告诉我,我会编辑。我会检查。谢谢顺便问一下,linq对group by+having的翻译是什么?就像你在上面写的那样:将行[Color]按行[productPrice]分组到价格中?希望能有所帮助。基本上,它按行[productPrice]对所有行[Color]值进行分组,因此每个价格都成为一个组,该价格的每个颜色都放在该组中。我查一下。谢谢顺便问一下,linq对group by+having的翻译是什么?就像你在上面写的那样:将行[Color]按行[productPrice]分组到价格中?希望能有所帮助。基本上,它按行[productPrice]对所有行[Color]值进行分组,因此每个价格都成为一个组,该价格的每个颜色都放在该组中。