How do I write an exists subquery using multiple columns in LINQ? - c#

I am trying to write a linq query that resembles this SQL:
SELECT * FROM Table1
WHERE EXISTS (
SELECT 1 FROM Table2
WHERE Table1.ColA = Table2.ColA
AND Table1.ColB = Table2.ColB
)
Except Table2 is an object list I already have previously from the database.
I know how to use contains() to emulate an SQL "IN SUBQUERY" using a object list outside of the database when one column is involved:
var query = from t1 in db.Table1
where MyObjList.Select(o => o.Field1).Contains(t1.Col1)
select t1;
I figure I can do a join in Linq. But will that perform ok? I hope avoid a database call per object in my list.

var q = from t1 in db.Table1
from t2 in db.Table2.Where(x => x.ColA == t1.ColA && x.ColB == t1.ColB)
select t1;

Try like this:
var query = from t1 in db.Table1
join t2 in db.Table2 on t1.ColA equals t2.ColA
Where t1.ColB == t2.ColB
Select t1;
OR without Join
var query = from t1 in db.Table1
from t2 in db.Table2
Where t1.ColA == t2.ColA && t1.ColB == t2.ColB
Select t1;

Related

Change sql query to linq in mvc

I want to change sql query to linq using join statement. The query should retrieve columns (days and startdate) while matching records of table 1 to table. In short converting sql query to linq using join statement.
Below is what i've tried.
SQL Query (Working)
SELECT *
FROM dbo."Batches"
INNER JOIN dbo.StudentBatchRelation
on dbo.Batches.Id = dbo.StudentBatchRelation.BatchId
WHERE
dbo.StudentBatchRelation.StudentId = '3d980306-e36e-4581-8c98-219717cb1668'
LINQ (not fetching result)
var result = (from t1 in contBatch.GetallBatchList()
join t2 in contStudent.getAllStudentBatchList()
on t1.Id equals t2.batchId where t2.studentId == studentid
select new { t1.Days, t1.BatchDate }).ToList();
If your EF entities are well defined, you could simplify your query with :
var result = Db.Batches
.Include(p => p.StudentBatchRelation)
.Where(p => p.StudentBatchRelation.StudentId = "3d980306-e36e-4581-8c98-219717cb1668")
.ToList();
Otherwise, if you have to use your Getallxxxx functions, you could do :
var result = (from t1 in contBatch.GetallBatchList()
join t2 in contStudent.getAllStudentBatchList()
on t1.Id equals t2.batchId
where t2.studentId == "3d980306-e36e-4581-8c98-219717cb1668"
select t1)
.ToList();

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)

SQL to LINQ - left join from same table using values equal to and greater than

I have the following SQL query which I am trying to convert to LINQ.
SELECT t1.*
FROM table1 t1
LEFT JOIN table1 t2
ON (t1.MusicId = t2.MusicId AND t1.MusicDetailId > t2.MusicDetailId)
WHERE t2.MusicDetailId IS NULL and t1.SingerId = 2
ORDER BY t1.MusicId
I have tried the following but I am not getting the correct data back.
var query =
from t1 in table1
from t2 in table1
where t1.MusicId == t2.MusicId && t1.MusicDetailId > t2.MusicDetailId
where t1.SingerId == 2 && t2.MusicDetailId == null
orderby t1.MusicId
select t1;
Is anyone able to help to get this SQL query converted to LINQ correctly?
var query = from t1 in table1.Where(X=> X.SingerId == 2)
join t2 in table1.Where(X=>X.MusicDetailId ==null) on t1.MusicId equals t2.MusicId
where t1.MusicDetailId > t2.MusicDetailId
select t1 ;

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};

Linq To SQL LEFT JOIN (OR Statement)

I want to rewrite this simple MS SQL Query in Linq To SQL:
SELECT * FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.ID = T2.Table1ID OR T1.FirstName = T2.FirstName
How do I rewrite this in Linq To SQL?
Try this, although I don't know how well Linq-to-SQL will translate it:
from t1 in ctx.Table1
from t2 in ctx.Table2
.Where(t => t1.ID == t.Table1ID ||
t1.FirstName == t.Firstname)
.DefaultIfEmpty()
select new {t1, t2}
I don't believe this can be done because I don't think you can do the OR part of the join. The way you do joins in L2S would be (roughly)
join .. on
new {
T1.ID,
T1.FirstName
} equals new {
T2.Table1ID,
T2.FirstName
}
but that would match both.
Only thing I can think you could do would be to do some sort of subquery in there. But that's probably not what you're looking for. Sklivvz's suggestion may be the best one.
This is an inner join.
from t1 in ctx.Table1
from t2 in ctx.Table2
where t1.ID == t2.Table1ID ||
t1.FirstName == t2.Firstname
select t1
To get a left join it looks like you use DefaultIfEmpty(), per MSDN.
from t1 in ctx.Table1
from t2 in ctx.Table2.DefaultIfEmpty()
where t1.ID == t2.Table1ID ||
t1.FirstName == t2.Firstname
select t1

Categories

Resources