开发者

What's the easiest way to sort an EF4 EntityCollection<T>?

I'd love to add some sorting to an EntityCollection that is bound to an ItemsControl (in xaml). I'd also like to do it as simply as possible. It appears that this is not possible.

If I wrap the collection in a "sorted" version of the collection property within the Entity I lose collection change notifications. I can't use a CollectionViewSource because the entity collection's BindingListCollectionView does not support sorting for some goddamned reason (note: I've seen the blog po开发者_StackOverflow中文版st with the "dirty" hack to get around this, so please don't answer with that kthx).

Is there a simple (couple lines of xaml, couple lines of code, whatever) way to achieve this??


The EntityCollection type cannot be directly filtered or sorted. It's a common LINQ-to-Entities problem, see: Sort child objects while selecting the parent using LINQ-to-Entities

One solution would be to sort the entity collection separately using LINQ when you need the data, and incur the additional performance hit. If you're working with a collection you expect to be small and/or infrequently used, the difference in processing time could be negligible.

If you want the database perform the sorting and make use of any indexes, you can project the main entity along with the child entities. Alex James posts an example in his MSDN blog: http://blogs.msdn.com/b/alexj/archive/2009/02/25/tip-1-sorting-relationships-in-entity-framework.aspx. You're not limited to anonymous types, of course.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜