how to do grouping in LINQ
I'm trying to do a LINQ query to a list of RelatedProducts objects. I have to group them by CategoryId so I'm using the group by clause. Unfortunatly I just realized I can't simply use 'select g.Value' at the end. The group is a IGrouping and I want to return just the RelatedProduct here. How can I do this?
Also in my case a product can be in 2 categories at once, so how can I prevent LINQ from returning duplicates? I'm only interested in the first category (actualy it doesn't matter as long it's assigned to that product).
var query = from rp in context.RelatedProducts join p in context.Products on rp.ProductId2 equals p.ProductId join pc in context.ProductCategories on p.ProductId equals pc.ProductId where rp.ProductId1 == productId1 && !p.Deleted && (showHidden || p.Published) orderby rp.DisplayOrder group rp by pc.CategoryId into g select g;