动态排序和过滤预计的 Entity SQL 结果

发布于 2024-09-26 19:07:58 字数 812 浏览 1 评论 0原文

我有以下查询,我想使用投影类名称对其进行排序或过滤:

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 相同的 ProjectCountId 列动态排序此查询,例如 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还给你自由 2024-10-03 19:07:58

您应该能够这样做: .OrderBy(p => p.ProjectCount)

You should be able to do it like this: .OrderBy(p => p.ProjectCount)

很糊涂小朋友 2024-10-03 19:07:58

看一下动态 LINQ 库。
这是一个 教程 似乎描述了您的情况。

Take a look at the Dynamic LINQ library.
Here is a tutorial that seems to describe your case.

小嗷兮 2024-10-03 19:07:58

如果您知道该怎么做,您可以使用 LINQ 做一些有趣的事情。也许是这样的?

        var query = 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, 
            });

        if (orderBy) // orderBy is bool, that tels you what to order by
            query = query.OrderBy(x => x.ProjectCount);
        else
            query = query.OrderBy(x => x.Id);

        list.AddRange(query);

You can do some nifty things with LINQ, if you know what to do. Maybe something like this??

        var query = 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, 
            });

        if (orderBy) // orderBy is bool, that tels you what to order by
            query = query.OrderBy(x => x.ProjectCount);
        else
            query = query.OrderBy(x => x.Id);

        list.AddRange(query);
老旧海报 2024-10-03 19:07:58

感谢您的回复,我使用了辅助扩展方法:

使用字符串作为列名在 IQueryable 中排序

Thanks for the replies, I used helper extension methods:

Sorting in IQueryable using string as column name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文