开发者

What should be the return type for the WCF service method having a master-detail Linq-to-entities query?

I have a service method written as follows for a Silverlight 4 application:

    [OperationContract]
    public List<string> GetAll()
    {
       GearsLtdEntities ge = new GearsLtdEntities();
       var query = from a in Employees 
                    join b in Depts
                    on a.DeptID equals b.DeptId
                    group a by b.DeptId into c
                    select new
                    {
                        DeptId = c.Key,
                        Name =
                              from cg in c
                              group cg.Name by 开发者_运维百科cg.Name into g
                              select new
                               {
                                  Name = g.Key
                               }
                   };
        
        return query;
}

and I want the data to be returned in this manner:

What should be the return type for the WCF service method having a master-detail Linq-to-entities query?

Now the query works fine when I run it standalone in LINQPad but when I am confused how to return the results back from the method. What should the method type be? I tried IEnumerable <DeptEmployee>, then I tried IEnumerable <string>, also tried a class DeptEmployee specifically created for this, but nothing seems to work.

Edit

I looked up projections and WCF data services as well, but couldn't find a single complete example demonstrating these, what I see are fragments and snippets and these aren't helping me get what I want. Is there a complete working example of using a master-detail query in WCF and SL with linq to entities and then returning the result from the service method?


Do you have a relationship between Employees and Depts on your edmx / designer? (You should!)

Why not just return a collection of Depts, where each Dept has its own collection of Employees?

[OperationContract]     
public List<Depts> GetAll()
{        
    GearsLtdEntities ge = new GearsLtdEntities();        
    return ge.Depts.Include("Employees").ToList();
}

Silverlight can easily retrieve the relevant properties from the objects.

The only reason that I wouldn't do this is if either Employee or Dept contained a significant chunk of data that you didn't want to send across the wire. If that's the case then create cut-down classes to represent these two objects.


It is an anonymous type so you need to use projection. I think that this post will help. Thanks. http://programming4.us/database/96.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜