How to combine in linq for sql?

Hi how do you connect in linq with sql?

I can see they have the join keyword and group join, but what are they?

Left, right, cross, inside?

Also how can I select specific columns as soon as I'm done?

how

Select Column1, Column2
From Table A;

The query or method syntax is fine. I still can't decide what I like, each seems to have its own pros.

+2


source to share


3 answers


As a first step, I would definitely recommend reading Scott Guthrie's excellent Using the Linq-to-SQL Multilingual Tutorial . It covers many of these Linq-to-SQL fundamentals.

Also check out Connected to View 5 minutes in LINQ - as mentioned there, often in Linq-to-SQL, you really don't even need to express JOINs yourself - LINQ-to-SQL handles many of them for you directly, exposing entity references and references on a set of objects.

If you're done, you really MUST use an explicit JOIN, Bilal Haydar shows perfectly how to do an explicit LEFT OUTER JOIN in his blog post.

The "normal" INNER JOIN case is relatively simple - you just add

from c in customers
  join o in orders on o.customerid equals c.customerid

      

to your request.

As for selecting individual fields - see the article on the series in Scott Guthrie's book - he explains it in great detail!

Basically, you can do one of three things:

  • select the whole object (for example, customer) and return all information for it.
  • select some fields to a new existing type (like CustomerOrder type)
  • select some fields to a new anonymous type


This will select all customers who have an order:

   from c in customers
      join o in orders on o.customerid equals c.customerid
      select c;

      

This will select all customers and orders into a new type "CustomerOrder" that already exists in your project somewhere:

   from c in customers
      join o in orders on o.customerid equals c.customerid
      select new CustomerOrder { CustomerName = c.Name, 
                                 CustomerID = c.CustomerId,
                                 OrderDate = o.OrderDate, ...... } ;

      

This will select the customer ID and order date into a new anonymous type to use:

   from c in customers
      join o in orders on o.customerid equals c.customerid
      select new { CustomerID = c.CustomerId, OrderDate = o.OrderDate } ;

      

It's a strong type - and there is a full-fledged .NET CLR type behind it - you just don't know its name :-) But you can use these elements in your code, you get Intellisense and that's it - great for fast temporal processing of some data.

Mark

+4


source


Check out the MSDN article publication on LINQ-To-SQL connections: http://msdn.microsoft.com/en-us/library/bb425822.aspx#linqtosql_topic12

A LINQ join is an inner join.



A group join in LINQ is a union that contains a clause. Parent information is combined with child information groups. That is, the child information is merged into a collection, while the parent information of the child collection happens only once.

0


source


The join keyword in LINQ will give you an inner join. You can "project" your results by creating a new object in the select clause. This object can be entered either anonymously. Here's an example:

var ordersAndProducts = from o in orders
    join p in products on o.ProductId equals p.ProductId
    select new { OrderDate = o.OrderDate, ProductName = p.Name };

      

This query performs an inner join on orders and product collections in the ProductId property. Each item in the resulting collection is projected into an anonymous object (the new {..list of properties / values ​​...} creates an object on the fly.

If you are using LINQ to SQL (and it looks like you are), you can see a SELECT statement that is sent to your database by debugging your application or by running a profiler on your database.

0


source







All Articles