locked
When people use join using EF RRS feed

  • Question

  • User1183902823 posted

    with the help of include function we can fetch related data. so tell me a scenario when i need to use join using EF because include does the join when fetch data.

    please tell me a situation when people will not use include rather use or compose join using EF?

    IEnumerable<Order> order= _ctx.order
                .Include(x => x.orderdetails)
                .Where(x => x.OrderID== _OrderID).ToList();

    Wednesday, January 3, 2018 12:13 PM

Answers

  • User753101303 posted

    Hi,

    Include and join are not really related. I'm using join only when  I don't have a navigation property I could use (ie very rare). It is sometimes used when not really needed when you still think about your Linq queries with a SQL rather than with a C# point of view (ie joining rows rather than using references to other objects).

    IMO trying to find a scenario to understand each feature is perhaps not the best way. It's perhaps best to really look at what include and join is really about (ie eager loading and just "linking" data) and then you'll better understand when you can use them.

    To me using join for eager loading is a kind of side effect rather than really the purpose of a join (I likely very rarely used that if ever at all, only maybe if "reshaping" my data can make sense).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2018 12:40 PM
  • User753101303 posted

    Yes, if I have a navigation property I'm using it. In the rare case where I don't have navigation properties I can use to "link" data I have no other option than using join.

    Edit: also maybe if you really need an inner join ? (but once again if you embrace a C# perspective you expect rather a parent object with an empty collection rather than to have it skipped because it doesn't have any child)

    Linq is about using C# to query your db, not about bringing SQL into C#.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2018 5:15 PM

All replies

  • User753101303 posted

    Hi,

    Include and join are not really related. I'm using join only when  I don't have a navigation property I could use (ie very rare). It is sometimes used when not really needed when you still think about your Linq queries with a SQL rather than with a C# point of view (ie joining rows rather than using references to other objects).

    IMO trying to find a scenario to understand each feature is perhaps not the best way. It's perhaps best to really look at what include and join is really about (ie eager loading and just "linking" data) and then you'll better understand when you can use them.

    To me using join for eager loading is a kind of side effect rather than really the purpose of a join (I likely very rarely used that if ever at all, only maybe if "reshaping" my data can make sense).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2018 12:40 PM
  • User1183902823 posted

    so you mean to say we have to use join when there is no navigational property between two class?

    Wednesday, January 3, 2018 2:39 PM
  • User753101303 posted

    Yes, if I have a navigation property I'm using it. In the rare case where I don't have navigation properties I can use to "link" data I have no other option than using join.

    Edit: also maybe if you really need an inner join ? (but once again if you embrace a C# perspective you expect rather a parent object with an empty collection rather than to have it skipped because it doesn't have any child)

    Linq is about using C# to query your db, not about bringing SQL into C#.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2018 5:15 PM