views:

1026

answers:

2

Hi there, 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  A: 

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

TTT
Yea i've read this thread and still don't find a way to make it work with my problem :o|
Simon
A: 

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.

Craig Stuntz
Ahh nice! Then i don't need to make a "new item()" !! I juste have to return a list of the selected objects and that it! Haha, "Why do easily when I can make it difficult" ... Quebec proverb.
Simon
Ho well, after check back my code, it's not working! Sorry! In this case, i want to get a list of "TransactionDetails" ordered by number of time that a "TransactionDetails.Chanson" where downloaded ... I think i MUST do a group by and gettin a cout of that.
Simon
It doesn't sound to me (again, without seeing your model) like you need a group by. You say "it's not working." Can you explain precisely *what* is not working?
Craig Stuntz
Yea well sorry. It's hard to me to explain correctly. Not that its not working, its because it do not do the good things. I explain : People download musik by doing a "Transaction". Every albums are linked to a "TransactionDetails". But how can i know how ofter an albums were downloaded :o|!
Simon
Are you saying that you want a combined list of songs and albums, and your current query is returning songs only, and not albums? If so, remove "details.Chanson != null" and change Item so that IdItem is nullable (as well as any other properties on Item, since not all returned records will have songs).
Craig Stuntz
Yea transactiondetail can have song or album linked to. But if user, in a transaction, had selected 2 song and 1 albums, another 2 other and the same album, and [...], I want to have a list of most downloaded songs and another list of most download albums. But, for now, saying that we've only have "chanson".
Simon
The query I gave you should work for songs. I'll try a query for albums, but I'll have to guess at data model property names for that.
Craig Stuntz
Dah! My fault sorry, it works preaty fine. Thanks Graig realy helpfull. !!
Simon