开发者

Silverlight - LinqToEntities - How Do I Return Anonymous Types

开发者 https://www.devze.com 2023-01-29 05:13 出处:网络
I\'m not sure if I\'m going at this the right way.I have a Silverlight app and using Entity Framework for a lot of it.I have Two Entities mapped to my database:Header and Details.I want to submit a le

I'm not sure if I'm going at this the right way. I have a Silverlight app and using Entity Framework for a lot of it. I have Two Entities mapped to my database: Header and Details. I want to submit a left outter join to get all headers and details counts - even if the header record has no detail record. Here is the Linq Query I want to run from the Client:

var query =
      from head in storeContext.Headers
      join detail in storeContext.Details
      on head.HeadId equals details.HeadId
      into group
      select new 
      {
        Desc = head.Description,
        MyCount = group.Count()
      };

Since this is Silverlight, I need to build my query, then submit it to the server with the storeContext.Load<T>() method from my Domain Service (Context on the client). Because this method is expecting a type, I don't know how to structure the call to return an anonymous type as I am dong above?

Am I doing 开发者_开发百科this all wrong? Should I be using an Invoke method for something like this? If so, how and where do I define the type I want to return?

Is someone could point me in the right direction I would really appriciate it.

Thanks... Scott


You can't return an anonymous type. They are, by necessity, local to the current scope.

Your query looks correct, but you will need to return a named type:

var query =
      from head in storeContext.Headers
      join detail in storeContext.Details
      on head.HeadId equals details.HeadId
      into group
      select new MyHelper // SPECIFY A CLASS HERE
      {
        Desc = head.Description,
        MyCount = group.Count()
      };

Your query method will need to return an IEnumerable<MyHelper>:

public IEnumerable<MyHelper> GetInfo()
{
    var query ...

    return query;
}


You can never return a value that is an instance of an anonymous type from a method. Anonymous types are always local to the method. If you want to return a type over WCF you're going to have to write the class yourself, rather than relying on anonymous types.

0

精彩评论

暂无评论...
验证码 换一张
取 消