What is the return type for a anonymous linq query select? What is the best way to send this data back?

允我心安 提交于 2019-12-05 11:17:21

An anonymous type is like any other class, but it's created by the compiler. What the compiler generates is something like:

class AnonymousType1 {
  public AnonymousType2 Key { get; set; }
  public int ProductCount { get; set; }
}

class AnonymousType2 {
  public int y { get; set; }
  public int m { get; set; }
  public int d { get; set; }
}

Those classes are not accessible to you so you have no choice but to use a custom class matching the definition of Anonymous1 instead if you want to keep strong typing. You'll then use it like this: new MyClass { Key = myGroup.Key, ProductCount = mygroup.Count() }.

Try using a KeyValuePair

public IQueryable<KeyValuePair<ApplicationLog,int>> GetApplicationLogsGrouped()
{
  var x = from c in ObjectContext.ApplicationLogs
          let dt = c.LogDate
          group by c  into mygroup
          select new KeyValuePair<ApplicationLog,int>( mygroup.Key,mygroup.Count()) ;

  return x;

  // return this.ObjectContext.ApplicationLogs.Where(r => r.ApplicationID < 50);
}

You'll need to create your own class for the projection.

public class ApplicationLogStatistic
{
    public ApplicationLog ApplicationLog { get; internal set; }
    public int ProductCount { get; internal set; }
}

...

public IQueryable<ApplicationLogStatistic> GetApplicationLogsGrouped()
{
    var x = // OP's code, except for select
        select new ApplicationLogStatistic 
                  { 
                       ApplicationLog = mygroup.Key, 
                       ProductCount = mygroup.Count() 
                  };
    return x;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!