Stellen Sie eine FrageStellen Sie eine Frage
 

BeantwortetHow to filter Include in ObjectQuery

  • Donnerstag, 7. Mai 2009 20:13Dynamic TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Background:
    ObjectQuery<Customer> myCollection = myEntityFrameworkContext.Customer.Include("Order.OrderDetail");

    (Each Customer has a collection of Order objects and each Order has a collection of OrderDetail objects)

    Question
    How can I filter the result or change the query in a way that only those resultsets are returned where OrderDetail with the productId = 2.

    something like this: myEntityFrameworkContext.Customer.Include("Order.OrderDetails").Where("it.Order.OrderDetail.productId = 2")

    Any help is much appreciated.

Antworten

  • Freitag, 8. Mai 2009 09:10Dynamic TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    I found the solution myself :)

    myEntityFrameworkContext.Customer.Include("Order.OrderDetails").Where(customer => customer.Order.Any(order => order.OrderDetail.Any(orderDetail => orderDetail.ProductId ==2)));
    • Als Antwort markiertDynamic Freitag, 8. Mai 2009 09:10
    •  
  • Samstag, 4. Juli 2009 20:31Dynamic TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    I think this is a bug with the Entity Framework or I don't know how to do it.

    Anyway, the only way I found is to change the Object Query direction.

    The "many" side of the relationship must come first always:

    myEntityFrameworkContext.OrderDetail.Include("Order.Customer").Where(orderDetail.ProductId ==2);

    Now only the Order and Customer are returned which are associated with the selected OrderDetail.

    Now again I have to Convert the data to the format I need which means Customer.Order.OrderDetail not OrderDetail.Order.Customer
    • BearbeitetDynamic Samstag, 4. Juli 2009 20:43
    • Als Antwort markiertDynamic Samstag, 4. Juli 2009 20:31
    •  

Alle Antworten

  • Freitag, 8. Mai 2009 09:10Dynamic TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    I found the solution myself :)

    myEntityFrameworkContext.Customer.Include("Order.OrderDetails").Where(customer => customer.Order.Any(order => order.OrderDetail.Any(orderDetail => orderDetail.ProductId ==2)));
    • Als Antwort markiertDynamic Freitag, 8. Mai 2009 09:10
    •  
  • Freitag, 3. Juli 2009 16:38Dynamic TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

    Well, I just noticed a problem with this.

    Customer is filtered but other tables are returned entirely! (not filtered)

    Any idea?

  • Samstag, 4. Juli 2009 20:31Dynamic TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    I think this is a bug with the Entity Framework or I don't know how to do it.

    Anyway, the only way I found is to change the Object Query direction.

    The "many" side of the relationship must come first always:

    myEntityFrameworkContext.OrderDetail.Include("Order.Customer").Where(orderDetail.ProductId ==2);

    Now only the Order and Customer are returned which are associated with the selected OrderDetail.

    Now again I have to Convert the data to the format I need which means Customer.Order.OrderDetail not OrderDetail.Order.Customer
    • BearbeitetDynamic Samstag, 4. Juli 2009 20:43
    • Als Antwort markiertDynamic Samstag, 4. Juli 2009 20:31
    •