Ask a questionAsk a question
 

AnswerHow have it works the EntityColletion?

  • Thursday, November 05, 2009 6:55 PMMarcos Aguiar Jr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    I my project, I have an entity that have an Entity Collection and, I want to filter the colletion.
    For example, I do a LINQ that brings an customer and this customer have an EntityColletion with all his orders this customer. I want to filter something orders, Is this possible?

    Thanks in Advance.

    Marcos Aguiar Jr - Brazil

Answers

  • Friday, November 06, 2009 5:12 AMAkhil Karkera - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Marcos,

    I hope I understood your question correctly.

    If you have a handle on the customer, you can issue a query as follows to filter some of the orders
    Customer cust = YourMethodToGetACustomer()

    foreach (Order o in cust.Orders.Where(x => x.OrderId > 0))
    {
          //do something with the order
    }

    You can use the followng link to get more information on using LINQ to Entities:
    http://msdn.microsoft.com/en-us/library/bb738458.aspx


    Regards,
    Akhil Karkera (MSFT)
  • Friday, November 06, 2009 5:58 AMSyed Mehroz Alam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Marcos,

    If you want to get some customers records and filtered collection of the orders belonging to those customers, you can have a query like this:

    var query =
       from c in dc.Customers
       where c.Active == true //some condition
       select new {
          c, 
          orders = from o in c.Orders
                    where o.OrderDate > DateTime.Parse("2009-10-01") //filter orders of each customer on some condition
                    select o
       };
    
    var customers = query.AsEnumerable().Select(q => q.c);
    
    //now customers will have selected customers and their associated filtered orders
    


    For more details, have a look at this post: How to do a Conditional Include


    Regards,

    Syed Mehroz Alam
    My Blog | My Articles

All Replies

  • Friday, November 06, 2009 5:12 AMAkhil Karkera - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Marcos,

    I hope I understood your question correctly.

    If you have a handle on the customer, you can issue a query as follows to filter some of the orders
    Customer cust = YourMethodToGetACustomer()

    foreach (Order o in cust.Orders.Where(x => x.OrderId > 0))
    {
          //do something with the order
    }

    You can use the followng link to get more information on using LINQ to Entities:
    http://msdn.microsoft.com/en-us/library/bb738458.aspx


    Regards,
    Akhil Karkera (MSFT)
  • Friday, November 06, 2009 5:58 AMSyed Mehroz Alam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Marcos,

    If you want to get some customers records and filtered collection of the orders belonging to those customers, you can have a query like this:

    var query =
       from c in dc.Customers
       where c.Active == true //some condition
       select new {
          c, 
          orders = from o in c.Orders
                    where o.OrderDate > DateTime.Parse("2009-10-01") //filter orders of each customer on some condition
                    select o
       };
    
    var customers = query.AsEnumerable().Select(q => q.c);
    
    //now customers will have selected customers and their associated filtered orders
    


    For more details, have a look at this post: How to do a Conditional Include


    Regards,

    Syed Mehroz Alam
    My Blog | My Articles