简体   繁体   中英

Is it necessary to use joins in Linq to Sql

I was practicing today when I realized that there are two ways linq to sql can retrieve data from db, I created two datagrid and used the two different ways to populate each of these datagrids and they produced the same result.

The first method is using joins to get data from related tables, and the other methods is using linq query like an object to access related tables. The code is shown below:

NorthWindDataContext dbContext = new NorthWindDataContext();

        var orders = from ord in dbContext.Orders
                     select new { ord.ShipCountry , ord.Customer.ContactName};

        var orders2 = from ord in dbContext.Orders
                     join cust in dbContext.Customers on ord.CustomerID equals cust.CustomerID
                     select new
                     {
                         ord.ShipCountry, cust.ContactName
                     };
        var data = orders2;

        DataGrid.ItemsSource= orders;
        DataGrid2.ItemsSource = orders2;

My question like the title is if it is entirely necessary to use joins , because I find them really cumbersome to use sometimes.

You need to use something that gets you from the order to the customer.

Join can do this. This is how the second query works.

Having the order "know" about the customer can do this. This is how the first query works.

If your data provider is aware of the connection between order and customer then these will amount to the same thing.

If your data provider is not aware of the connection, then the approach in the first example would result in N + 1 look ups instead of 1.

A linq-friendly ORM will generally be aware of these connections as long as the appropriate relationship-marking attributes are present (just what that is differs between Linq2SQL, EF, NHibernate, etc.).

It's still important to know the join approach for cases where either the relationship isn't known about by the provider, or you have a reason to join on something other than a foreign-key relationship.

The answer is " sort of ". Since you're using an ORM such as Linq-to-Sql, no you don't directly need to call join within your linq queries to accomplish what you're trying to do.

However, when the ORM activates the query it will generate actual SQL code that'll have a join statement in it to get the results you're querying. Since you're using an ORM though, the data returned is mapped to objects, and since Customer has a relationship between the objects, the relationship will also be translated to from the database INTO the objects.

ord.Customer.ContactName

The above statement is most likely translated to a JOIN statement performing an INNER JOIN between Customer & Orders.

Due to this, both of your LINQ queries most likely generating similar SQL queries. Both of which has a JOIN statement in them. Because the relationships between your objects also exists within the database (and everything is mapped together showing this relationship) you don't directly need to use join within a LINQ statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM