none
using where clause RRS feed

  • Question

  • Hello, I'm a newbye

    if I have a order class that has a list of orderrow class that in turn has a list of surface class with a property called Name,
    how can I get the surface class of the order with name = "The Name" in linq?

    Thanks a lot!
    Sunday, September 20, 2009 9:30 AM

Answers

  • In that case:
    string name = "The Name";
    Surface sf = basket.OrderRows.Where(r => r.Surfaces.Any(s => s.Name == name)).First().Surfaces.Where(s => s.Name == name).FirstOrDefault();
    MVP XML My blog
    • Marked as answer by AndreaPi Sunday, September 20, 2009 1:05 PM
    Sunday, September 20, 2009 12:52 PM

All replies

  • Surface sf = db.Order.Where(o => o.Orderrow.Any(r => r.Surface.Any(s => s.Name == "The Name"))).Select(o => o.Orderow.First().surface.First()).FirstOrDefault();

    might do but I am guessing a lot. You might better post the definition of the classes you have.
    MVP XML My blog
    Sunday, September 20, 2009 10:02 AM
  • Sorry, you're right but what you wrote is good.

    So, Let's specify better: I call the OrderRow list  OrderRows and the list of surface Surfaces.
    The only big difference I can't fix is the fact i haven't a db but a class called basket in place of db.Order.
    How can you fit your previos query to this scenario?

    Again, a big thanks!
    Sunday, September 20, 2009 11:39 AM
  • What exactly is "basket"? The name of a class? Or a variable holding a collection of Order objects? In the latter case you could try

    string name = "The Name";

    Surface sf = basket.Where(o => o.OrderRows.Any(r => r.Surfaces.Any(s => s.Name == name))).Select(o => o.OrderRows.Where(r => r.Surfaces.Any(s => s.Name == name)).First().Surfaces.Where(s => s.Name == name).First()).FirstOrDefault();
    MVP XML My blog
    Sunday, September 20, 2009 12:42 PM
  • basket is an hinstance of Order class.
    Sunday, September 20, 2009 12:45 PM
  • In that case:
    string name = "The Name";
    Surface sf = basket.OrderRows.Where(r => r.Surfaces.Any(s => s.Name == name)).First().Surfaces.Where(s => s.Name == name).FirstOrDefault();
    MVP XML My blog
    • Marked as answer by AndreaPi Sunday, September 20, 2009 1:05 PM
    Sunday, September 20, 2009 12:52 PM