Convert a left join LINQ query from Comprehension to Lambda syntax - entity-framework

Is it possible to convert below LINQ query from Comprehension to "Lambda" syntax, that is table1.where().select().
from t1 in table1
from t2 in table2.Where(t2=>t2.Table1ID == t1.ID).DefaultIfEmpty()
select new {t1.C1, t2.C2}
Above query will be translated to a left join in SQL without using the ugly Join keyword in LINQ.

LinqPad gives this translation:
Table1
.SelectMany (
t1 =>
Table2
.Where (t2 => (t2.Table1ID) == t1.ID)
.DefaultIfEmpty (),
(t1, t2) =>
new
{
C1 = t1.C1,
C2 = t2.C2
}
)

Related

Filter entities with a field from another entity in linq query c#

I wanna join some tables in linq query. The problem is that I can not filter one of entities with field of another entity. In the below code b.createDate is not defiend, how can I do this query in linq?
From a in context.A
Join b in context.B
On a.Id equals b.AId
Join c in context.C.where(x =>
x.createDate >= b.createDate)
On b.Id equals c.BId into g
From result in
g.DefaultIfEmpty()
Select result
You have to use from instead of join in this case. As documented: Collection selector references outer in a non-where case
var query =
from a in context.A
join b in context.B on a.Id equals b.AId
from c in context.C
.Where(x => x.BId == b.Id && x.createDate >= b.createDate)
.DefaultIfEmpty()
select c;

sql multiple join (right. left, inner) to mongodb aggregate

//I have a sql like this
SELECT
x.attr1,
p.attr2,
p2.attr3,
o.attr4,
p2.attr5,
a2.attr6
FROM TABLE_O o
RIGHT JOIN TABLE_A a ON a.id = o.id AND a.id2 IS NULL
LEFT JOIN TABLE_A a2 ON a2.id = o.id AND a2.id2 = a.id3
LEFT JOIN TABLE_P p ON a.id4 = p.id4
LEFT JOIN TABLE_P p2 ON a2.id4 = p2.id4
INNER JOIN TABLE_X x ON a.id3 = x.id3
WHERE o.type = 'someText' AND a2.id4
IN ( SELECT id3 FROM TABLE_P WHERE TYPE = "anotherText")
AND x.attr1 = "someText"
//I need an idea about how can i do this on mongo aggregates, thanks.
nosql is a non-relational database type. but The following link may be useful.
mongodb aggregation

How to use OR in join while using Entity Framework

Can any one convert following SQL statement to Entity Framework C# or VB.net for me?
SQL statement:
select t1.*, t2.*
from tblWISTransacs t1
inner join tblWCBTransacs t2 on t1.TicketNo = t2.TicketNo
or t1.TicketNo = t2.customernumber
var result = (from t1 in dbContext.tblWISTransacs
join t2 in dbContext.tblWCBTransacs on 1 equals 1
where (t1.TicketNo == t2.TicketNo || t1.TicketNo == t2.customernumber)
select new { t1, t2 }).ToList();
The generated SQL is a bit different from Palani Kumar's, but you could also use
from t1 in db.tblWISTransacs
from t2 in db.tblWCBTransacs
where t1.TicketNo == t2.TicketNo || t1.TicketNo == t2.customernumber
select new { T1 = t1, T2 = t2 }
I think both end up essentially as a cross join.

Slick 3.0.0 Aggregate Queries

I'm trying to do a complex querying on two tables. They look like below:
Table1:
id:
name:
table2Id:
version:
Table2:
id:
comments:
Assuming that I have the appropriate Slick classes that represents these tables, I'm trying to get all the elements from Table1 and Table 2 that satisfies the following condition:
Table1.table2Id === Table2.id and max(Table1.version)
I tried the following:
val groupById = (for {
elem1 <- table1Elems
elem2 <- table2Elems if elem1.id === elem2.id
} yield (elem1, elem2)).groupBy(_._1.id)
I know that I have to map the groupById and look for the max version, but I'm not getting the syntax right! Any help?
What I need is effectively Slick equivalent of the following SQL query:
SELECT *
FROM t t1
WHERE t1.rev = (SELECT MAX(rev) FROM t t2 WHERE t2.id = t1.id)
To simulate:
SELECT * from t t1 WHERE t1.rev = (SELECT max(rev) FROM t t2 WHERE t2.id = t1.id)
Try either of the following:
val q1 = for{
t < table1 if t.rev === (
table2.filter(_.id === t.id).map(_.rev).max
)
} yield t
val q2 = table1.filter{t=> t.rev === (
table2.filter(_.id === t.id).map(_.rev).max
)}.map(identity)
EDIT based on comment:
val q = (for{
t1 < table1; t2 <- table2 if t1.id === t2.id
} yield t1).orderBy(_.version.max).reverse.take(1)

left join from a group of tables to another group of tables in postgresql

I have some tables say t1 , t2 , t3.
I need to implement something like this in postgresql.
select * from (t1 , t2) left join t3
where t1.some_column = t3.some_column;
But postgresql complains
ERROR: syntax error at or near "," SQL state: 42601 Character: 77
You can't use from (t1,t2), you have to join them in some way.
Try something like this:
select * from t1
inner join t2 on t1.someColumn=t2.someColumn
left join t3 on t1.some_column = t3.some_column;