如何使用 OData 和 LINQ 进行嵌套计数?

发布于 2024-09-13 06:24:14 字数 577 浏览 16 评论 0原文

这是我尝试从 OData 源运行的查询:

var query = from j in _auditService.AuditJobs.IncludeTotalCount()
   orderby j.Description
   select new 
      {
         JobId = j.ID,
         Description = j.Description,
         SubscriberCount = j.JobRuns.Count()
      };

如果我不使用 j.JobRuns.Count(),它运行得很好,但如果我包含它,我会收到以下错误:

构造或初始化实例 类型的 <>f__AnonymousType1`3[System.Int32,System.String,System.Int32] 使用表达式 j.JobRuns.Count() 不支持。

尝试通过OData获取嵌套计数似乎是一个问题。有什么解决办法吗?我试图避免仅仅为了获得计数而获取每个对象的整个嵌套集合。

谢谢!

Here is the query I am trying to run from my OData source:

var query = from j in _auditService.AuditJobs.IncludeTotalCount()
   orderby j.Description
   select new 
      {
         JobId = j.ID,
         Description = j.Description,
         SubscriberCount = j.JobRuns.Count()
      };

It runs great if I don't use the j.JobRuns.Count(), but if I include it I get the following error:

Constructing or initializing instances
of the type
<>f__AnonymousType1`3[System.Int32,System.String,System.Int32]
with the expression j.JobRuns.Count()
is not supported.

It seems to be a problem of attempting to get the nested count through OData. What is a work around for this? I was trying to avoid getting the whole nested collection for each object just to get a count.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

悲凉≈ 2024-09-20 06:24:14

截至目前,OData 协议不支持聚合。

预测是的,但包含聚合属性的预测则不是。

亚历克斯

As of today the OData protocol doesn't support aggregates.

Projections yes, but projections that include aggregate properties no.

Alex

您需要 .Net 4.0 并且在 LinqPad 中您可以通过 netflix OData 服务运行以下命令

void Main()
{
    ShowPeopleWithAwards();
    ShowTitles();
}

// Define other methods and classes here
public void ShowPeopleWithAwards()
{
    var people = from p in People.Expand("Awards").AsEnumerable()
            where p.Awards.Count > 0
            orderby p.Name
            select new
            {
              p.Id,
                    p.Name,
              AwardCount = p.Awards.Count,
             TotalAwards = p.Awards.OrderBy (a => a.Type).Select (b => new { b.Type, b.Year} )
                 };

    people.Dump();
}

public void ShowTitles()
{
    var titles = from t in Titles.Expand("Awards").AsEnumerable()
            where t.ShortName != string.Empty && 
                  t.ShortSynopsis != string.Empty &&
             t.Awards.Count > 0
            select t;
    titles.Dump();
}

You need .Net 4.0 and In LinqPad you can run following over netflix OData Service

void Main()
{
    ShowPeopleWithAwards();
    ShowTitles();
}

// Define other methods and classes here
public void ShowPeopleWithAwards()
{
    var people = from p in People.Expand("Awards").AsEnumerable()
            where p.Awards.Count > 0
            orderby p.Name
            select new
            {
              p.Id,
                    p.Name,
              AwardCount = p.Awards.Count,
             TotalAwards = p.Awards.OrderBy (a => a.Type).Select (b => new { b.Type, b.Year} )
                 };

    people.Dump();
}

public void ShowTitles()
{
    var titles = from t in Titles.Expand("Awards").AsEnumerable()
            where t.ShortName != string.Empty && 
                  t.ShortSynopsis != string.Empty &&
             t.Awards.Count > 0
            select t;
    titles.Dump();
}
冷情妓 2024-09-20 06:24:14

您现在可以使用我的产品 AdaptiveLINQ 和扩展方法 QueryByCube

You can now use my product AdaptiveLINQ and the extension method QueryByCube.

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