如何在实体框架中包含排序的导航属性

How to include sorted navigation properties with Entity Framework(如何在实体框架中包含排序的导航属性)
本文介绍了如何在实体框架中包含排序的导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体 A,里面有一个 B 的集合.我用 _entity.A.Include(a => a.B)

I have an entity A with a collection of B inside. I load them with a _entity.A.Include(a => a.B)

现在我想将 B 放入 A 中,按自定义 OrderBy 排序.我试过 _entity.A.Include(a => a.B.OrderBy(o => o.Version) 但我得到一个:

Now I want to have the B's into A sorted by a custom OrderBy. I tried _entity.A.Include(a => a.B.OrderBy(o => o.Version) but I get a :

包含路径表达式必须引用在类型上定义的导航属性.对引用导航属性使用虚线路径,对集合导航属性使用 Select 运算符.

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

关于如何实现这一点的任何想法?

Any ideas on how to accomplish this?

谢谢.

版本是一个整数.

推荐答案

我觉得这种情况你可以试试:

I think in this case you can try:

var list = _entity.A.Include("B").ToList();
list.ForEach(m => m.B = m.B.OrderBy(o => o.Version));

或:

_entity.A.Include("B").Select(m => new A {
        //some props,
        B = m.B.OrderBy(o => o.Version)
        });

这篇关于如何在实体框架中包含排序的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)