1

I have a list of products with productname, productprice & category. I want to write a linq query to group all the products according to category. I have tried the following:

var p = from s in productlist
        group s by s.Category into g
        select new { Category = g.Key, Products = g};

With this query, it's showing a table with two column as category & product. Category column has two categories as expected but under products column, there is no data. I would love to have all the product list separated by the category.

ekad
  • 14,436
  • 26
  • 44
  • 46
Badhon Jain
  • 938
  • 6
  • 20
  • 38

1 Answers1

3

OK..Need a bit more help on this, now when I run the code, its just showing a table with categories column showing two categories as expected, but the product column not showing any data.

You need to select the products from the group:

Products = g.Select(p => p).ToList()

Have a look at following with some additional properties.

var categories = from s in productlist
                group s by s.category into g
                select new { 
                    Category = g.Key, 
                    Products = g.ToList(),
                    ProductCount = g.Count(),
                    AveragePrice = g.Average(p => p.productprice)
                };
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • sorry for that, but currently I'm having problem to ask new question. So I changed this question to a new one as per SO's suggestion. Your answer was right for the previous question, but for this one, I need some fresh attention, I thought if someone come to see the accepted answer, they will not make any further afford. Can you please suggest me other way to get rid of the limitation imposed by SO, I'm really sorry to mark it unaccept. – Badhon Jain Jun 19 '12 at 10:57
  • Why can't you ask new questions?? – Tim Schmelter Jun 19 '12 at 11:00
  • I have no idea! One of question was closed by some senior members, then suddenly I got the restriction notice! I know I'm asking some very basic questions since I'm new to C#, but didn't violate any other rules, in fact I really in love with this site & would like to contribute in every possible way. May be it will take some time. – Badhon Jain Jun 19 '12 at 11:03
  • @BadhonJain: You're getting the exception because you're selecting `p` which is out of context since you've grouped by an anonymous type. Note that you've also a semicolon at the end of the group by, that's why you're getting the _"missing using ..."_ exception. But you need also a `into group` statement. Finally you need to select the group, you cannot select the `product`. For example: `from p in product group p by new {category=p.category, pricegroup= p.productprice} into grp select grp;` – Tim Schmelter Jun 19 '12 at 11:09
  • Glad to hear that it works now. But i haven't heard of any restrictions to ask new questions. I would recommend to ask for the reason on [Meta Stackoverflow](http://meta.stackoverflow.com/). I'll rollback your question to the last version since my answer makes no sense anymore otherwise. – Tim Schmelter Jun 19 '12 at 11:23
  • I'm curious as to why `Products = g.Select(p => p).ToList()` works when `Products = g` doesn't. (Also as to why you have `g.Select(p => p).ToList()` but not `g.Select(p => p).ToList().Count()` or `g.Select(p => p).Average(p => p.productprice)`.) – Rawling Jun 19 '12 at 11:42
  • @Rawling: 1.) you can omit the `ToList()` if you want, it's just to materialize the `IEnumerable` to a `List` 2.) `Products = g` does work but it's type is a `Lookup.Grouping` and i assumed that OP wanted a `IEnumerable` or `List` instead 3.) You can use the group to select aggregations like count or average (or the `List`), but i wouldn't use it to iniliaze an anonymous type's property which will represent a list of products. – Tim Schmelter Jun 19 '12 at 11:53
  • @Tim - bah, I copied/pasted wrong. I was more curious as to why you had the `Select(p => p)` in there. – Rawling Jun 19 '12 at 11:56
  • 1
    Right, I can see why you'd use `g.Select(p => p)` to give the anon type an IEnumerable, or `g.ToList()` to give it a List, but why both? – Rawling Jun 19 '12 at 12:04
  • @Rawling: Now i understand what you mean, yes, i could also omit the `Select` if i use `ToList` anyway. But it doesn't hurt. I'll edit my answer, thanks. – Tim Schmelter Jun 19 '12 at 12:13