"Group By" and/or "Order By" in the entity framework

asp.net c# entity-framework visual-studio-2008

Question

Say we've got a project that allows user to download things. On the main page, I want to show the Most downloaded files ordered by the number of download! All that using EF.

How can i do this !! I've tried many things with Group By (Its a nightmare when you've got a lot of informations in an object). And i still dunno how to do this...

var query = from details in m_context.TransactionDetails
                        where details.Chanson != null
                        group details by details.Items into AnItem
                        orderby AnItem.Count()
                        select new Item() {
                            IdItem = Chansons.Key.IdItem,
                            ItemState= Chansons.Key.ItemState,
                            [...This object got something like 20 including links to other objects ... ]
                        };

Anyone have an idea?

Thanks :o)

Oh and sorry for my english, I'm giving my best but im from Quebec (Usualy talk french).

1
4
10/13/2009 8:44:26 PM

Accepted Answer

Salut!

I'm going to guess at your data model a little, here, but I don't think you need to group:

var query = from details in m_context.TransactionDetails
            where details.Chanson != null
            orderby details.Items.Count() descending
            select new Item
            {
                IdItem = details.Chanson.IdItem,
                ItemState= details.Chanson.ItemState,
                // ...
            };

Bonne chance!

Update: For albums:

var query = from details in m_context.TransactionDetails
            where details.DisqueCompact != null
            orderby details.Items.Count() descending
            select new Item 
            {
                IdItem = details.DisqueCompact.IdItem,
                ItemState= details.DisqueCompact.QuelqueChose...
                // ...
            };

You probably need two queries given your data model.

2
10/14/2009 8:35:52 PM

Popular Answer

For grouping data, you can read this How-To from MSDN.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow