LINQ implements various connections to T-SQL

Source: Internet
Author: User

In the ORM framework of the big line of the today, for the. NET industry people, want to learn EF, that LINQ learning is imperative AH. Today summarizes the common use of table connections.

Inner Join

Linq:

1   var list = ( from in customerdb.order2in                               Customerdb.orderitem on C.orderid equals O.orderitemid3                             Selectnew {c = c. OrderId}). FirstOrDefault ();

The generated SQL

1 SELECT TOP(1) 2 [Extent1].[OrderId]  as [OrderId]3  from  [dbo].[Orders]  as [Extent1]4 INNER JOIN [dbo].[OrderItems]  as [Extent2]  on [Extent1].[OrderId] = [Extent2].[Orderitemid]

Left Join

      LINQ:

 1  var  list = (from  c in   Customerdb.order  2  join o in   custome Rdb.orderitem on C.orderid equals o.orderitemid  3   into GRP  4  from  grp in   Grp.defaultifempty ()  5  select  
   
    new  {c = c.orderid, GRP = grp. Orderitemid}). FirstOrDefault (); 
   

Generated sql:

1 SELECT TOP(1) 2 [Extent1].[OrderId]  as [OrderId], 3 [Extent2].[Orderitemid]  as [Orderitemid]4  from  [dbo].[Orders]  as [Extent1]5  Left OUTER JOIN [dbo].[OrderItems]  as [Extent2]  on [Extent1].[OrderId] = [Extent2].[Orderitemid]

Cross Join

Linq:

1     var list = ( from in customerdb.order2                               3                             Selectnew {c = c.orderid,o=o.orderitemid}). FirstOrDefault ();

Generated sql:

1 SELECT TOP (123[extent2].[ Orderitemid] as [Orderitemid]4from  [dbo].[ Orders] as [Extent1]5 cross JOIN [dbo]. [OrderItems] As [Extent2]

Summary: The right connection is actually the order position of the two tables replaced,

The whole connection is to put the results of the left outer join together to go heavy on the line.

With Shun EF, you can speed up the development efficiency, look forward to the early arrival of the day, refueling Ah!!!

LINQ implements various connections to T-SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.