c# - 如何通过 Entity Framework 按动态值排序?

标签 c# asp.net entity-framework

我正在使用 Entity Framework 构建一个 asp.net mvc 应用程序,并且我正在尝试按列表排序。通过动态更改名称,根据数据库中存在的名称。

bids = bids.OrderBy(s => s.PublisherName);

和对象:

public string PublisherName { get { db.Publishers.Find(pubid).Name; } }

但我遇到了一个异常(exception):

The specified type member 'PublisherName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

我能做什么?我怎样才能让它发挥作用?

谢谢。

最佳答案

Only initializers, entity members, and entity navigation properties are supported.

db.Publishers.Find(pubid).Name; 既不是初始化程序,也不是实体成员,也不是导航属性。

一种可能的方法是将其记入内存 AsEnumerable() :

bids = bids.AsEnumerable().OrderBy(s => s.PublisherName);

只要出价是一小部分对象,这就会完美地工作。

关于c# - 如何通过 Entity Framework 按动态值排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34062121/

相关文章:

.net - 如何从 Entity Framework 中删除关系

c# - 添加到 Lambda 表达式并使用 Entity Framework

c# - 为什么 EF 在表中插入重复项?

c# - 有没有办法在 C# 中创建匿名结构?

c# - 如何使用 MySqlBulkLoader 添加列映射

asp.net - 从 ASP.NET 网站创建独立的 Windows 应用程序

asp.net - 64位机器上系统内存不足异常?

c# - 哈希和模数字符串在 C# 和 Python 之间具有相同的结果

C# - 文件关联对注册表项 'HKEY_CLASSES_ROOT\' 的访问被拒绝

jquery - 如何使用 AJAX 将数组发布到 Web 服务