How do you write an INNER JOIN with an "OR" in Linq - linq

I am trying to write a Linq query to generate the following SQL
SELECT
[t1].[Id], [t2].[value3]
FROM
[Table1] AS [t1]
INNER JOIN
[Table2] AS [t2] ON [t1].[Id] = [t2].[value1]
OR [t1].[Id] = [t2].[value2]
I have seen lots of examples for how to do multiple joins, but none for how to do this type of "one or the other" join.

var result = from t1 in context.Table1
from t2 in context.Table2
where (t1.Id == t2.value1 || t1.Id == t2.value2)
select new
{
t1.Id,
t2.value3
};

INNER JOIN
var query =
from t1 in context.Table1
from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
select new
{
t1.Id,
t2.value3
};
LEFT JOIN
var query =
from t1 in context.Table1
from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
.DefaultIfEmpty()
select new
{
t1.Id,
t2.value3
};

Related

how to do left join in lambda

var item= (from t1 in _dbEntities.PurchaseSales
join t2 in _dbEntities.ItemTypes on t1.ItemTypeID equals t2.ID
where t2.ID.Equals(null)
select t2).ToList();
how to do left join in it
Use DefaultIfEmpty:
var item= (from t1 in _dbEntities.PurchaseSales
join t2 in _dbEntities.ItemTypes on t1.ItemTypeID equals t2.ID into t
from l in t.DefaultIfEmpty()
where l == null
select t1).ToList();

Want to change SQL query to LINQ

I want to change the below simple SQL query into LINQ , how do I change it ?
select * from table1 where isPaid = 'true' and Id in (select Id from table2 where EmployeeId = 12)
similar to this ?
from pa in db.PaymentAdvices
where pa.IsPaid == true
orderby pa.PaidDate descending
select pa;
Here code linq to sql:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12
select t1
Hope usefull!
if the field isPaid has datatype is Boolean:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid == true
select t1
if the field isPaid has datatype is String:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid.Equals("true")
select t1

Linq and foreign key Lookup tables with integer keys

I have a database where the tables are:
Table1:
Table1Id int
Table2:
Table2Id int
ForeignKeyToTable1 int
LookupTable:
Table2Id
Table3Id
Table3:
Table3Id int
Table3Field varchar
I want to:
select table1.* from table1
inner join table2 on table1.Table1Id = ForeignKeyToTable1
inner join LookupTable on LookupTable.Table2Id = table2.Table2Id
inner join Table3 on table3.Table3Id = LookupTable.Table3Id
where table3.Table3Field ='qwerty'
How can this be achieved in Linq?
var query =
from table1 in db.Table1
join table2 in db.Table2 on new { Table1Id = table1.Table1Id } equals new { Table1Id = table2.ForeignKeyToTable1 }
join lookuptable in db.LookupTable on table2.Table2Id equals lookuptable.Table2Id
join table3 in db.Table3 on lookuptable.Table3Id equals table3.Table3Id
where
table3.Table3Field == "qwerty"
select new {
table1.Table1Id
};
I believe something like this will go
var query = from t1 in context.Table1
join t2 in context.Table2
on t1.Table1Id equals t2.ForeignKeyToTable1
join lt in context.LookupTable
on t2.Table2Id equals lt.Table2Id
join t3 in context.Table3
on lt.Table3Id equals t3.Table3Id
where t3.Table3Field == "qwerty"
select t1;

Join and batch update in LINQ to SQL

Can something like this be written in LINQ to SQL:
UPDATE Table1
SET CustomerName = t2.Name
FROM Table1 t1 JOIN Table2 t2
ON t1.TableID = t2.TableID
var result = from t1 in context.Table1
join t2 in context.Table2 on t1.TableID = t2.TableID
select new {t1, t2};
result.ToList().ForEach(i => i.t1.CustomerName = i.t2.Name);
context.SubmitChanges();

LINQ Join Where Clause

I'm struggling with a join/where clause with what is a rather simple sql select statement.
I am trying to retrieve a list of product information from tb1 with the where condition behind situated in tbl2 but this must be joined by three different columns.
so the SQL would look something along the lines of:
SELECT tb1.*
FROM tb2 INNER JOIN
tb1 ON tb2.Col1 = tb1. Col1 AND tb2.Col2 = tb1. Col2 AND
tb2.Col3 = tb1.Col3
WHERE (tb2.Col1 = col1) AND (tb2.Col2 = col2) AND (tb2.Col4 = string)
ColX is the main where clause with the string to be passed in as parameter; all other columns are within the contexts.
How do you implement multiple joins with a where clause?
And shoves in the right direction, muchly appreciated.
To join on multiple field in LINQ, you have to create a new anonymous type containing the columns you want to compare and then use that anonymous type in the join:
var results = from t1 in context.tb1
join t2 in context.tb2
on new { t1.Col1, t1.Col2, t1.Col3 } equals
new { t2.Col1, t2.Col2, t2.Col3 }
where t2.Col1 == col1 && t2.Col2 == col2 && t2.Col4 == someString
select t1;
And here is the equivalent Lambda Syntax:
var results = context.tb1.Join(
context.tb2,
t1 => new { t1.Col1, t1.Col2, t1.Col3 },
t2 => new { t2.Col1, t2.Col2, t2.Col3 },
(t1, t2) => new { t1, t2 })
.Where(o => o.t2.Col1 == col1
&& o.t2.Col2 == col2
&& o.t2.Col4 == someString)
.Select(o => o.t1);
As you can see, in the case of joins, query syntax usually produces an easier to read statement.
You can also include the WHERE clause in lamda syntax in the reference to the table you're joining on.
var query = from pt in dc.ProjectTasks
join ttab in dc.TimeTaskAssigns on pt.Id equals ttab.ProjectTaskId
join ttb2 in dc.CMS_TAT_TIMEs.Where(a => a.WIP_STATUS == 'B') on ttab.CmsTimeUno equals ttb2.TIME_UNO
select pt;
Seems obvious now, doesn't it? It took me a long time to find that solution.
Also you can group result and use subquery
var innerGroupJoinQuery2 =
from category in categories
join prod in products on category.ID equals prod.CategoryID into prodGroup
from prod2 in prodGroup
where prod2.UnitPrice > 2.50M
select prod2;
ref.: join clause (C# Reference)

Resources