2010-08-07 36 views
1

这里是我想从我的OData源运行查询:如何用OData和LINQ进行嵌套计数?

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()运行很好,但如果我把它给我得到以下错误:

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.

这似乎是一个试图通过OData获取嵌套计数的问题。什么是解决这个问题的方法?我试图避免为每个对象获取整个嵌套集合以获得计数。

谢谢!

回答

1

截至今日,OData协议不支持聚合。

预测是的,但包含聚合属性号码的投影。

亚历

0

你需要.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(); 
}