Convert LINQ query to SQL - c#

var list = (from t1 in table1 ✓
join t2 in table2 on t1.xyz equals t2.abc ✓
join t3 in table3 on new { t1.abc , t2.qwe} equals new { t3.abc , t3.qwe}
select new Table
{
XYZ= t1.xyz,
ABC = t1.abc,
QWE= t3.qwe
}).Distinct().ToList();
I want to convert this C# LINQ query to SQL query.
join t3 in table3 on new { t1.abc , t2.qwe} equals new { t3.abc , t3.qwe}
I couldn't convert after this part. Is there anyone who can help me?

Here:
SELECT DISTINCT t1.xyz AS XYZ, t1.abc AS ABC, t3.qwe AS QWE
FROM table1 t1
JOIN table2 t2 ON t1.xyz = t2.abc
JOIN table3 t3 ON t1.abc = t3.abc AND t2.qwe = t3.qwe

This could be your desired SQL Query
SELECT DISTINCT t1.xyz AS XYZ, t1.abc AS ABC, t3.qwe AS QWE
FROM table1 t1
JOIN table2 t2 ON t1.xyz = t2.abc
JOIN table3 t3 ON t1.abc = t3.abc AND t2.qwe = t3.qwe

Related

Incorrect results from multiple joins in Linq2SQL

I've been tasked with converting some crusty old SQL to Linq2SQL and I know this shouldnt be the first choice however it needs to be done.
Problem
I'm stuck attempting to force specific joins to replicate the desired output and the following is a simplified version of the SQL with one parameter variant shown followed by a simplified version of my attempt in Linq2SQL. Several alternative fields may be queried in Table1 hence the use q and q2 in the converted code though the actual parameters are not relevant to the issue.
Aim
The query needs to retrieve relevant rows from Table1, the latest related row from Table2 and additional data from Table2's parent table Table3, preferably in a single pass.
There may be 0-n matches in Table1, Table1 has a 1:n relationship with Table2 with 0-n rows, there is a 1:n relationship between Table3 and Table2.
Regardless of how I structure the expressions, the linq generates an INNER JOIN onto Table2 excluding rows in Table1, how can I structure the linq query to achieve the desired result?
SELECT [...]
FROM Table1 t1
LEFT JOIN (
SELECT MAX(id) AS id, parent_id
FROM Table2
GROUP BY parent_id
) x2 ON t1.id = x2.parent_id
LEFT JOIN Table2 t2 ON x2.id = t2.id
LEFT JOIN Table3 t3 ON t2.table3_id = t3.id
WHERE t1.id = row_id
var q = dc.Table1.AsQueryable();
q = from r in q where r.id == row_id select r;
var q2 = from r in (q)
join x2 in (from r in dc.Table2.DefaultIfEmpty() group r by r.parent_id into maxt2 let max_id = maxt2.Max(f => f.id) select new { maxt2.Key, max_id }) on r.id equals wx.Key
join t2 in dc.Table2.DefaultIfEmpty() on x2.max_id equals t2.id
join t3 in dc.Table3.DefaultIfEmpty() on t2.table3_id equals t3.id
select
{
[...]
};
Here is my translation using my recipe rules:
var Q1 = from t2 in dc.Table2
group t2 by t2.parent_id into t2g
select new { parent_id = t2g.Key, id = t2g.Max(t2 => t2.id) };
var Q2 = from t1 in Table1
where t1.id == row_id
join q1 in Q1 on t1.id equals q1.parent_id into q1j
from q1 in q1j.DefaultIfEmpty()
join t2 in dc.Table2 on q1.id equals t2.id into t2j
from t2 in t2j.DefaultIfEmpty()
join t3 in dc.Table3 on t2.table3_id equals t3.id into t3j
from t3 in t3j.DefaultIfEmpty()
select new { t1, t2, t3 };

How to add || (OR) condition with linq join

How to convert the below SQL query into LINQ query for C#.net
Select t1.id,t2.Name
From table1 t1
INNER JOIN table t2
ON ((t1.column3 is null and t1.id = t2.id)
OR( t.Column3 is NOT NULL and t1.column3 = t3.Column3))
Join tblXYZ xyz on t1.column4 = xys.columnn2
I was unable to add or condition after first set up comparison in linq query, please suggest correct way to achieve this in linq.
Making some assumptions about what you meant, I would suggest hoisting the OR to a union:
(from t1 in table1
join t2 in table2 on t1.Column3 equals t2.Column3
join xyz in tblXYZ on t1.Column4 equals xyz.column2
where t1.Column3 != null).Union(
from t1 in table1
join t2 in table2 on t1.id == t2.id
join xyz in tblXYZ on t1.Column4 equals xyz.column2
where t1.Column3 == null)

Linq to SQL: How to join on no field i.e. cartesian join

How can I make a Linq To SQL join on multiple tables where 1 table should produce a Cartesian product.
To shed some more light, here is a sample of the SQL query.
SELECT Table1.MyField, Setup.SomeField
FROM Table1 INNER JOIN Table2 ON Table1.SomeField = Table2.SomeField, Setup
My Linq to SQL are like:
var q = from t1 in db.Table1
join t2 in db.Table2 on t1.SomeField equals t2.SomeField
join setup in db.Setup
select new {t1.MyField, setup.SomeField};
I'm getting an error on the last join that Type inference failed in the call to 'Join'.
Use SelectMany rather than a Join to perform a Cartesian Product.
In query syntax that would be:
var query = from t1 in db.Table1
from t2 in db.Table2
select new {t1, t2};
This will also do:
var q = from t1 in db.Table1
join t2 in db.Table2 on t1.SomeField equals t2.SomeField
from setup in db.Setup
select new {t1.MyField, setup.SomeField};

Using result of let in where clause LinQ

I am using Linq To SQL to get data from from data tables. I want to use the result of a let statement in a where clause.
eg:-
from t1 in table1
join t2 in table2
on t1.field1 equals t2.field2
let calculatedValue = t2.val1 + t2.val2
join t3 in table3
on t2.somefield equals t3.somefield
into t3Grp
from subt3 in t3Grp.DefaultIfEmpty()
[select the row in table3 with where table3.someValue < calculatedValue]
The proble I face in calculatedValue is not accessible in .Where() clause of table3. I want fully left join output( ie; even if there is no row in table3 having someValue < calculatedValue i want that row in the output. Please help.
Try This:
from t1 in table1
join t2 in table2
on t1.field1 equals t2.field2
let calculatedValue = t2.val1 + t2.val2
join t3 in table3.where(item => item.someValue < calculatedValue)
on t2.somefield equals t3.somefield
into t3Grp
from subt3 in t3Grp.DefaultIfEmpty()
select new
{
val1= table3.val1,
val2 = table3.val2,
.
.
.
}
Can you do this by restricting t3Grp in your final select, e.g.
from t1 in table1
join t2 in table2
on t1.field1 equals t2.field2
let calculatedValue = t2.val1 + t2.val2
join t3 in table3
on t2.somefield equals t3.somefield
into t3Grp
from subt3 in t3Grp.DefaultIfEmpty()
select new { t1, t2, t3Grp = t3Grp.Where(t3 => t3.someValue < calculatedValue) }
The value is in scope there, I'm just not sure of the exact projection you want.

Linq to SQL translation to SQL syntax

I'm trying to do the following query in Linq
SELECT *
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.ID = T2.AnotherID
LEFT OUTER JOIN Table3 T3
on T1.ID = T3.AnotherID
It works as expected in proper SQL syntax, but i'm having a hard time translating it to the corresponding Linq to SQL syntax.
How do i combine left join with an inner join?
Regards,
var results = from t1 in Table1
from t2 in Table2
where t1.ID = t2.AnotherID
join t3 in Table3 on t1.ID equals t3.AnotherID into joined
from j in joined.DefaultIfEmpty()
select new {t1, t2, t3 = j}

Categories

Resources