views:

266

answers:

3

This is a basic question. I have the basic SL4/RIA project set up and I want to create a new method in the domain service and return some data from it. I am unsure the proper easiest way to do this.. Should I wrap it up in a ToList()? I am unclear how to handle this anonymous type that was create.. what is the easiest way to return this data?

 public IQueryable<ApplicationLog> GetApplicationLogsGrouped()
    {
        var x = from c in ObjectContext.ApplicationLogs
                let dt = c.LogDate
                group c by new { y = dt.Value.Year, m = dt.Value.Month, d = dt.Value.Day } into mygroup
                select new { aaa = mygroup.Key, ProductCount = mygroup.Count() };

        return x;


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

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<CapRep4.Web.ApplicationLog>'. An explicit conversion exists (are you missing a cast?) 58 20 CapRep4.Web

+2  A: 

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);
}
alejandrobog
public IQueryable<KeyValuePair<ApplicationLog, int>> GetApplicationLogsGrouped() { var x = from c in ObjectContext.ApplicationLogs let dt = c.LogDate group c by new { y = dt.Value.Year, m = dt.Value.Month, d = dt.Value.Day } into mygroup select new KeyValuePair<ApplicationLog, int>( mygroup.Key, mygroup.Count()); return x; Argument 1: cannot convert from 'AnonymousType#1' to 'CapRep4.Web.ApplicationLog'
punkouter
missing something ?
punkouter
I just edit my answer, try it
alejandrobog
that doesnt work... expects the BY keyword
punkouter
+3  A: 

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() }.

Julien Lebosquain
I sort of understand. but then what would the signature of the method be ?
punkouter
+1  A: 

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;
}
Robert Paulson
public IQueryable<ApplicationLogStatistic> GetApplicationLogsGrouped2() { var x = from c in ObjectContext.ApplicationLogs let dt = c.LogDate group c by new { y = dt.Value.Year, m = dt.Value.Month, d = dt.Value.Day } into mygroup select new ApplicationLogStatistic { oApplicationLog = mygroup.Key, ProductCountz = mygroup.Count() }; return x; }
punkouter
Cannot implicitly convert type 'AnonymousType#1' to 'CapRep4.Web.ApplicationLog' 80 43 CapRep4.WebCannot implicitly convert type 'System.Collections.Generic.IEnumerable<CapRep4.Web.ApplicationLogStatistic>' to 'System.Linq.IQueryable<CapRep4.Web.ApplicationLogStatistic>'. An explicit conversion exists (are you missing a cast?) 84 20 CapRep4.Web
punkouter
Application Log is a ENTITY OBJECT but mygroup.key is a Tkey?
punkouter