动态排序和过滤预计的 Entity SQL 结果
我有以下查询,我想使用投影类名称对其进行排序或过滤:
List<CompanyInfo> list = new List<CompanyInfo>();
using (var db = new DbContext())
{
list.AddRange(
db.Companies.Include("Projects")
.Select(row => new CompanyInfo()
{
ProjectCount = (from s in row.Projects
where s.Company.fId == row.fId
select s.pId).Count(),
Id = satir.fId,
})
//.OrderBy("ProjectCount") //<== what I want to do
);
}
我想使用与 ESQL 相同的 ProjectCount
或 Id
列动态排序此查询,例如 .OrderBy("ProjectCount")
.由于查询结果是 IQueryable 而不是 ObjectContext,因此它不起作用。有办法做到这一点吗?
I have the following query which I want to sort or filter using projected class names:
List<CompanyInfo> list = new List<CompanyInfo>();
using (var db = new DbContext())
{
list.AddRange(
db.Companies.Include("Projects")
.Select(row => new CompanyInfo()
{
ProjectCount = (from s in row.Projects
where s.Company.fId == row.fId
select s.pId).Count(),
Id = satir.fId,
})
//.OrderBy("ProjectCount") //<== what I want to do
);
}
I want to dynamically order this query using ProjectCount
or Id
columns same as ESQL, like .OrderBy("ProjectCount")
. Since the query result is IQueryable instead of ObjectContext it doesn't work. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该能够这样做:
.OrderBy(p => p.ProjectCount)
You should be able to do it like this:
.OrderBy(p => p.ProjectCount)
看一下动态 LINQ 库。
这是一个 教程 似乎描述了您的情况。
Take a look at the Dynamic LINQ library.
Here is a tutorial that seems to describe your case.
如果您知道该怎么做,您可以使用 LINQ 做一些有趣的事情。也许是这样的?
You can do some nifty things with LINQ, if you know what to do. Maybe something like this??
感谢您的回复,我使用了辅助扩展方法:
使用字符串作为列名在 IQueryable 中排序
Thanks for the replies, I used helper extension methods:
Sorting in IQueryable using string as column name