tags:

views:

57

answers:

1

Hi, In my Parent Child scenario, I need to get the custom list of all the customers with their total number of orders and orderitems.

A Customer can have many orders and orders can have many OrderItems. I need to write a query which returns me the collection of entity Objects which has the Customer Entity and Total Orders and Total OrderItems for that Customer object e.g

var result = from c in  ctx.Customers
         from o in  ctx.Orders
         from d in  ctx.OrderItems
         select new List<CustomerVM()
         {
            customer = c,
            totalOrders = o.Count,
            totalOrderItems = XXXX  
         }  
public class CustomerVM {
 public Customer customer {get; set;}
 public int totalOrders {get; set;}
public int totalOrderItems {get; set;}
}

How can I do that ??

A: 

You need to use SelectMany:

var result = (from c in ctx.Customers
              select new CustomerVM
              {
                  customer = c,
                  totalOrders = c.Orders.Count(),
                  totalOrderItems = c.Orders.SelectMany(o => o.OrderItems).Count()
              }).ToList();
Yakimych
Thanks for prompt reply. I'm getting the following errorCannot Convert method group 'Count' to non-delegate type 'int' on c.Orders.SelectMany(o => o.OrderItems).Count
Jhelumi
Sorry, just use parenthesis: `Count()` instead of `Count`
Yakimych
Thanks for that. I'm running this in SL4 but its not returning any results. When i debug it I got the null value as result although there are 4 Customers with some orders as well.Any idea why is that? ctx is the DomainContext
Jhelumi
Do you get 4 results if you just query for Customers? `var result = ctx.Customers.ToList();`?
Yakimych
I'm afraid not. Its Count = 0 but Capacity = 4
Jhelumi
But if I do this then it works _customers = new PagedCollectionView(ctx.Customers); LoadCustomers(); public void LoadCustomers() { var qry = ctx.GetCustomersQuery(); LoadOperation loadOp = ctx.Load(qry); }
Jhelumi
What's in the `GetCustomersQuery()` method?
Yakimych
Is it something related to Load Operation that I need to use? because I'm not using that anywhere so its not returning anything. Please note that this is on Silverlight's client side code not the DomainService of Web/Server side.I did try to use the query in DomainService class but it was giving the error upon the custom object CustomerVM class NOT having the key attribute.
Jhelumi
return this.ObjectContext.Customers;
Jhelumi
Does it work if you change it to `this.ObjectContext.Customers.Select(c => new CustomerVM { customer = c, totalOrders = c.Orders.Count(), totalOrderItems = c.Orders.SelectMany(o => o.OrderItems).Count() })`?
Yakimych
I did try that before but as I said earlier that it does not approve the CustomerVM class and give the error that the 'entity CustomerVM in DomainService does not have a key defined.
Jhelumi
I got this working but the customer object is not populating within the retruned list. I'm only able to access the totalOrders and totalOrderItems.
Jhelumi
You can try to project the `Customer` object field by field unless you find a better solution: `.Select(c => new CustomerVM { customer = new Customer { Name = c.Name, ... }, totalOrders = c.Orders.Count() ... })`
Yakimych
I don't think that this is in select statement. if I look into the MetaData file, there is no property against the public Customer mainCustomer{get;set;} where as for all other there is equivalent one there. Is it because its EF defined entity and cannot be used here?I need to define the custom class as well to clone this
Jhelumi
Not sure I understand what exactly you are doing, but alternatively you can define the customer properties you need directly in the viewmodel: `public class CustomerVM { public string CustomerName {get; set;} ... public int totalOrders {get; set;} public int totalOrderItems {get; set;} }`, and then project onto it in a similar way: `.Select(c => new CustomerVM { customerName = c.Name, ... , totalOrders = c.Orders.Count() ... })`.
Yakimych
Yes that's what I'm thinking too. Actually I was trying to avoid that just because I don't want to duplicate all the Customer table. BUT i think that's the only way forward. Thanks again
Jhelumi