Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all columns after JOIN in LINQ

Tags:

c#

join

select

linq

I have two tables, Table1 and Table2. I want to perform, say, a left outer join:

var myOutput = from object1 in Table1
               join object2 in Table2
               on object1.Property1 equals object2.Property2 into Table3
               from output in Table3.DefaultIfEmpty()
               select new
                   {
                       object1.Property1,
                       object1.Property2,
                       //...
                       output.Property3,
                       output.Property4,
                       //...
                   };

As you can notice, I want to select all the properties of both objects from the resulting table (the enumerables considered while joining contain the objects of certain types - these are different for both relations). Of course, I can select the properties in the anonymous select, as shown in the example.

My question is how to avoid specifying all the properties manually? I would like to have something like SELECT * FROM TABLE3, where TABLE3 is a resulting relation (after joining TABLE1 and TABLE2).

Thanks in advance for the clues.

like image 777
Jamie Avatar asked Oct 22 '11 16:10

Jamie


People also ask

What is inner join in LINQ?

LINQ Inner Join Sometimes, it is required to send data from two or more tables or objects in a single unit based on your provided information. Inner Join produces the result from two or more than two tables. So, basically we are meant to get the records from both tables based on matching conditions.

What is an example of a LINQ join clause?

Examples include listing all Customers with their SalesOrders, including Customers that don't have a SalesOrder, and listing all Customers who don't have a SalesOrder. To meet these requirements you need to use the LINQ Join clause.

What are the different types of join in SQL?

There are different types of join in SQL and these are Inner Join, Left Outer Join, Right Outer Join, Full Outer Join, and Cross Join. But after introducing Linq with C# 3.0, there were huge changes in the programming world. Now, most of the developers use Linq for getting the data from the object. Linq stands for Language Integrated Query.

What is a LINQ query?

The following is the Linq query for the above SQL query. It includes all rows from the left table and matching rows from the right table. We process the query with some condition which contains the unique column in both tables.


1 Answers

You have to specify each manually if you want to project into a flattened type. Your other option is to just have your combined type contain both objects, and the objects will naturally bring along their properties.

select new 
{
    Object1 = object1,
    Object2 = output
};

And you would work with it like myObj.Object1.Property1, myObj.Object2.Property4, etc.

One final option that still involves some manual work is to define an appropriate type and have a constructor or a builder method that does the work of segmenting out your object properties into a flattened type. You still perform the manual mapping, but you isolate it from your query logic.

select new CombinedType(object1, output);
//or 
select builder.GetCombinedType(object1, output);
like image 72
Anthony Pegram Avatar answered Sep 20 '22 13:09

Anthony Pegram