Linq and foreign key Lookup tables with integer keys - linq

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;

Related

Can u help me convert this stored procedure to LINQ

ALTER Procedure [dbo].[GetItemsTypesByProduct]
(
#ProductID INT
)
As
Begin
Select distinct iMap.ProductId, iTyp.Id ItemTypeId, iTyp.ItemType, iTyp.SortOrder
from itemMapping iMap
inner join items itm on itm.itemid = imap.itemid
inner join itemType iTyp on iTyp.id = itm.ItemTypeId
Where ProductId = #ProductID
UNION
Select distinct rMap.ProductId, iTyp.Id, iTyp.ItemType, iTyp.SortOrder
from ReaderMapping rMap
inner join itemType iTyp on iTyp.id = 25
Where ProductId = #ProductID
UNION
Select distinct aMap.ProductId, iTyp.Id, iTyp.ItemType, iTyp.SortOrder
from AssessmentMapping aMap
inner join itemType iTyp on iTyp.id = 24
Where ProductId = #ProductID
order by productid, iTyp.SortOrder
Select distinct aTyp.AssessmentTypeId, AssessmentTypeName, MappingLevel
from eds_quiz..AssessmentType aTyp
inner join eds_quiz..Assessments asm on (asm.AssessmentTypeId = aTyp.AssessmentTypeId)
inner join AssessmentMapping aMap on (aMap.AssessmentId = asm.AssessmentId)
Where ProductId = #ProductID
Select Id, TypeName from eds_edusmart..lessontype
Where Id NOT IN (1, 2, 4)
End

How do you write an INNER JOIN with an "OR" in 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
};

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

Inner join in Linq with more than 2 datatables

i have 3 tables
t1==>
t1.ID t1.co1 t1.col2
1 a b
2 a b
t2==>
t2.ID t2.co1 t2.col2
1 a b
2 a b
t3==>
t3.ID t3.co1 t3.col2
1 a b
2 a b
i want inner join between all three tables using Linq and want selected column in 4th datatable.
equivalent sql query:
SELECT t1.ID,t2.col1,t3.col2
FROM t1
INNER JOIN t2 ON t1.ID=t2.ID
INNER JOIN t3 ON t1.ID=t3.ID
t4==>
t1.ID t2.co1 t3.col2
1 a b
2 a b
Something like this
var Result =
from row1 in t1
join row2 in t2 on row1.ID equals row2.ID
join row3 in t3 on row1.ID equals row3.ID
select new { ID = row1.ID, Col1 = row2.col1, Col2 = row3.col2 }
DataTable dt = Result.CopyToDataTable();
Use LoadDataRow() to get a DataTable from an anonymous type as here. Else Result.CopyToDataTable().
//Get the column list as same as table1 to new datatable
DataTable table4 = table1.Clone();
var Result =
from x in t1.AsEnumerable() join
y in t2.AsEnumerable() on x.Field<int>("ID") equals y.Field<int>("ID") join
z in t3.AsEnumerable() on x.Field<int>("ID") equals z.Field<int>("ID")
select new table4.LoadDataRow(
new object[] {
x.ID,
y.col1,
z.col2
}, false);

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();

Resources