0

I have 2 data tables:


Table1

ID: [1, 2]

Name: ["A", "B"]


Table2

ID: [1, 2, 3]

NameID: [1, 2, 2]

Action: ["test", "test", "don't test"]


If I want to get the following query:


Table2.ID: [1, 2, 3]

Table2.NameID: [1, 2, 2]

Table2.Action: ["test", "test", "don't test"]

Table1.ID: [1, 2, 2]

Table1.Name: ["A", "B", "B"]


I would write, in SQL, the following:

SELECT *
FROM Table2 LEFT JOIN Table1 ON Table2.NameID = Table1.ID

What's an equivalent expression I can write in LINQ?

I don't want to have to write the field names out. I would rather just write a Select All where the duplicates are immediately resolved by adding a table identifier to the qualifying fields.

I also do not want to return a query with two list fields.

Thus far, I've tried:

from x in Table2
join y in Table1
on x.NameID equals y.ID into grouping
select new { x, grouping }

But, it's returned a query of two list fields: x and grouping.

  • 1
    Please, follow the [Perform left outer joins](https://learn.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins) specs and show how did you load the tables – Pavel Anikhouski Mar 25 '20 at 18:43
  • @PavelAnikhouski It more or less returns the same things. Plus, the specs only show a query where the fields are explicitly stated in the select statement, and I would rather not have to specify the exact fields. –  Mar 25 '20 at 18:50
  • Perhaps my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) might help you. PS You must write out the list of fields, because C# and LINQ are strongly typed. It is possible to not do so using `DataTable` or Reflection, but it is extremely poor performance for less comprehensible coding. – NetMage Mar 25 '20 at 20:01

0 Answers0