group by related entity's field

Posts   
 
    
Aeon
User
Posts: 24
Joined: 21-Jul-2008
# Posted on: 21-May-2009 17:29:28   

C# 2.6.9.331

I'm trying to group on a related entity's field, which seems to be required in my case as I need to group by two fields, each on separate tables.

e.g.: var test = from orderDetail in this._linqMetaData.OrderDetails group orderDetail by orderDetail.Product.ProductCategoryId into groups select groups

This seems to be related to the following post: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=14386&HighLight=1

which states.... "grouping on fields in a related entity isn't supported in the form as you use it"

Is it supported at all? If so, could you provide an example?

Thanks, -Ian

Aeon
User
Posts: 24
Joined: 21-Jul-2008
# Posted on: 21-May-2009 18:22:33   

After some thought, the linq could more accurately represent the sql as follows:

var test = from orderDetail in this._linqMetaData.OrderDetails join product in this._linqMetaData.Product on orderDetail.ProductId equals product.ProductId group orderDetail by new {orderDetail.OrderId, product.ProductCategoryId} into groups select groups

which gives an error about converting an anonymousType to an Entity.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 21-May-2009 21:18:44   

Sorry, this is not supported as it requires a second query to create the hierarchy (the returned object is a tree, not a single set) and the second query isn't mergeable with the parent query because of the related entity grouping (so there's no common data element to link the sets on)

Matt

Aeon
User
Posts: 24
Joined: 21-Jul-2008
# Posted on: 21-May-2009 21:52:37   

Ok, thanks for the response.

Here is a workaround for me (as I don't require any aggregate functions on the groups).

var orderDetailProductCategory = from orderDetail in this._linqMetaData.OrderDetails select new { orderDetail.OrderId, orderDetail.Product.ProductCategoryId}

var test = from odpc in orderDetailProductCategory.Distinct() join productCategory in this._linqMetaData.ProductCategory on odpc.ProductId equals productCategory.ProductCategoryId join order in this._linqMetaData.Orders on odpc.OrderId equals order.OrderId select new {order.foo, productCategory.bar}

Hope this helps someone. Thanks again.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-May-2009 08:42:37   

Aeon, thanx for the feedback.