开发者

how do I use linq to dataSet to convert dataTable to an array?

开发者 https://www.devze.com 2023-03-13 06:23 出处:网络
I have t开发者_StackOverflowhe following sql table schema: procudtIdproductPriceColor ==================================

I have t开发者_StackOverflowhe following sql table schema:

procudtId   productPrice  Color
==================================
1           3 $           Blue
1           3 $           Red
1           3 $           Green
2           5 $           Blue
2           5 $           Red

Using c# code I got this into dataSet. How can I use linq to dataSet to build an array that looks like

[ price:"3$", ColorList:<"Blue","Red","Green"> ;
  price:"5$", ColorList:<"Blue","Red">]

Thanks


I think this will work:

    //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()
                };

If that doesn't do it, let me know and I'll edit.


I think I'll do this in two steps:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消