none
Limit child records when returning parent object with LINQ RRS feed

  • Question

  • Hi all,

     

    I am having some difficulty return an object with a limited amount of children.

     

    private static Customer GetCustomer (int customerId, CustomerContext context)
    {
           context.ContextOptions.ProxyCreationEnabled = false;
    
            var cs = from c in context.Customers
                         .Include("CustomerDetails")
                         .Include("CardDetails")
                         .Include("Orders")
                         where c.CustomerId == customerId
                         select c;
    
             return cs.FirstOrDefault();
    }
    

    Is there anyway to limit the amount of orders returned? Either the last 10, or the past years orders only within the LINQ statement?

     

    Thanks

     

     

     

    Sunday, October 23, 2011 5:40 PM

Answers

  • Hi Michael_PB;

    Using the Include method in the query will return ALL CustomerDetails, CardDetails and Orders for the filtered Customers. If you want to only return a filtered number of records you can either lazy load the Orders or return an anonymous type with the wanted records.

    var cs = from c in context.Customers
    	where c.CustomerId == customerId
    	select new {
    		Customer = c,
    		Orders = c.Orders.OrderByDescending (o => o.OrderDate ).Take(10)
    	}; 
    

    The above query will return at most the last 10 Orders filtered by OrderDate. You will need to do the same for CustomerDetails and CardDetails as well.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Michael_PB Sunday, October 23, 2011 9:11 PM
    Sunday, October 23, 2011 8:32 PM

All replies

  • Hi Michael_PB;

    Using the Include method in the query will return ALL CustomerDetails, CardDetails and Orders for the filtered Customers. If you want to only return a filtered number of records you can either lazy load the Orders or return an anonymous type with the wanted records.

    var cs = from c in context.Customers
    	where c.CustomerId == customerId
    	select new {
    		Customer = c,
    		Orders = c.Orders.OrderByDescending (o => o.OrderDate ).Take(10)
    	}; 
    

    The above query will return at most the last 10 Orders filtered by OrderDate. You will need to do the same for CustomerDetails and CardDetails as well.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Michael_PB Sunday, October 23, 2011 9:11 PM
    Sunday, October 23, 2011 8:32 PM
  • Thanks Fernandoo,

     

    I was afraid of that, LazyLoading isn't an option.

    I will have to load the object with no children, and load the children seperately when required.

     

    Thanks for the example with the anonamous type, I will be able to use that in a different part of the project.

    Sunday, October 23, 2011 9:15 PM
  •  

    Not a problem, glad to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, October 24, 2011 1:49 PM
  • Fernando wrote "...you can either lazy load the Orders or return an anonymous type with the wanted records.".

    Question:

     The answer gave an example of using an anonymous type.  How would one use Lazy Loading to restrict which children are loaded?   


    Sunday, February 9, 2014 9:46 PM