views:

242

answers:

2

Hi,

I have the following LINQ example:

var colorDistribution = 
    from product in ctx.Products
    group product by product.Color
    into productColors

    select
       new 
    {
       Color = productColors.Key,
       Count = productColors.Count()
    };

All this works and makes perfect sense.

What I'm trying to achieve is to group by into a strong type instead of anonymous type.

For example I have a ProductColour class and I would like to Group into a List<ProductColour>

Is this possible?

Thank you

+2  A: 

EDIT: Okay, I'd completely misread your post. By the looks of it, you're not wanting to group by a different type - you're wanting to project each element of the group into a different type. Here's what I'd do:

var colorDistributionSql = 
    from product in ctx.Products
    group product by product.Color
    into productColors

    select
       new 
    {
       Color = productColors.Key,
       Count = productColors.Count()
    };

var colorDistributionList = colorDistributionSql
      .AsEnumerable()
      .Select(x => new ProductColour(x.Color, x.Count))
      .ToList();
Jon Skeet
Right, lets give this a go...Thanks
vikp
@vikp: Please see the edit - I hadn't spotted this was SQL. I've now completely rewritten the answer, as your use of the phrase "group by" was confusing me. I see it's the *result* projection that you want to convert to a custom type, not the grouping projection.
Jon Skeet
Hey, thanks for the edit and the code snippet. In order to do this I won't have to override getHashCode() and equals() methods, right?
vikp
@vikp, no, because the GroupBy is performed by the DB, it doesn't actually execute any .NET code
Thomas Levesque
Sort of weird why LINQ2SQL doesn't allow `select new ProductColour(...)` directly...
Peter Lillevold
This line of code: .ToList(x => new ProductColour(x.Color, x.Count));generates a following exception: No overload for method 'ToList' takes 1 arguments
vikp
Do I understand this correctly? 1. We group by criteria, E.g. Color.2. We store the result of the query in a new anonymous type with two fields: Key and Count3. We convert whatever is stored in the anonymous variable and store it in the list.Is this considered as an elegant approach in LINQ? Thank you
vikp
@vikp: Hmm... I must have misremembered the overloads available. Will fix. But yes, that's the general approach.
Jon Skeet
Would be great if you could help out with the overload on a ToList(). In the mean time thank you for the help :)
vikp
@vikp See the edited code.
Jon Skeet
Rocknroll! Thanks!
vikp
+1  A: 

LinqToSql does allow direct projection of the group into a type (after all, the group is just an IGrouping<T, U>, a type). What LinqToSql can't do, is translate a constructor into SQL.

IQueryable<ProductColour> colorDistributionSql =  
    from product in ctx.Products 
    group product by product.Color 
    into productColors 
    select new ProductColour()
    { 
       Color = productColors.Key, 
       Count = productColors.Count() 
    };
David B