Ordering sub-items within ordered items in a Linq to Entities Query

只愿长相守 提交于 2019-12-20 03:56:13

问题


I have a few tables, Listings, ListingImages and a few others related to Listings. ListingImages is related to Listings so that you can have many ListingImages per Listing.

When I query this table I do;

IQueryable<Listing> ListingToSendToView = (from x in DBEntities.ListingSet.Include("ListingImages")
                                                                          .Include("OtherTables")
                                           where x.Listing.ID == (int)LID
                                           orderby x.ID descending
                                           select x).First();

Now this is fine. However, I now want to sort the ListingImages independently within each Listing (by an ImageOrder column I have in that table).

How can I do this and pass all my Includes(...). Would it be bad form to sort the ListingImages within the View as this solution seems to work?

Cheers


回答1:


I think you are going to have to sort it in the view, because if you want each group to have a different sorting, you have to do that manually. The only other option is to copy it to a dictionary or list structure and sort and copy it in the order, then return that from the controller... though I don't even know where to begin on that one.

I don't think sorting on the view is bad form, because it's part of the UI logic (or the UI's needs anyway)... it would be bad form to query data within the view, and be careful not to put too much logic in (I don't know the complexity you are talking about), but as long as its only sorting, I think that's OK.

HTH.



来源:https://stackoverflow.com/questions/3565249/ordering-sub-items-within-ordered-items-in-a-linq-to-entities-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!