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.
Related
I've tow contracts and one query I want to select one of them based on join result , without using where clause,
from a in pContext
join c in vContext
on a.id equals c.id into av
from lav in av.DefaultIfEmpty()
if(lav != null )
{
select new DTO1()
{
a.id,
a.name,
lav.description
}
}
else
{
select new DTO2()
{
a.id,
a.name
}
}
EF Core translates LINQ "left join" to SQL left join and you do not need to handle nulls in this case, because it expects values from DbDataReader. Simple projection should work:
var query =
from a in pContext
join c in vContext on a.id equals c.id into av
from lav in av.DefaultIfEmpty()
select new DTO1
{
a.id,
a.name,
lav.description
};
from a in pContext
join c in vContext
on a.id equals c.id into av
from lav in av.DefaultIfEmpty()
select new DTO()
{
id = a.id,
name = a.name,
description = lav != null ? lav.description : "No Specified"
}
The solution is based on Svyatoslav Danyliv comment using ternary operator but with different condition to avoid the NullReferenceException.
How can I write a linq to entities query that includes a group by and a having clause?
For example in SQL:
SELECT * FROM dbo.tblParent p
INNER JOIN
(
SELECT a.ID
FROM dbo.tblParent a
join dbo.tblChild c ON a.ID = c.FkParentID
WHERE a.ColValue = 167
GROUP BY A.ID
HAVING COUNT(c.ID) = 1
) t ON p.ID = t.ID
I found my own answer.
// this is far from pretty but it works as far as I can tell
Apps = (from x in context.tblParents
join t in (
from p in context.tblParents
join c in context.tblChilds
on p.ID equals c.FkParentID
where p.ColValue == 167
group c.ID by p.ID into grouped
where grouped.Count() == 1
select new { grouped.Key }) on x.ID equals t.Key
select x);
I wrote raw query to psql and it's work fine but when i wrote this in sqlalchemy my WHERE clause duplicated to FROM clause.
select id from T1 where arr && array(select l.id from T1 as l where l.box && box '((0,0),(50,50))');
In this query i fetch all id from T1 where array with ints intersects with results from subquery.
class T1():
arr = Column(ARRAY(Integer))
...
class T2():
box = Column(Box) # my geometry type
...
1 verison:
layers_q = select([T2.id]).where(T2.box.op('&&')(box)) # try find all T2 intersects with box
chunks = select([T1.id]).where(T1.arr.overlap(layers_q)) # try find all T1.id where T1.arr overlap with result from first query
SELECT T1.id
FROM T1
WHERE T1.arr && (SELECT T2.id
FROM T2
WHERE T2.box && %(box_1)s)
This i have a PG error about type cast. I understand it.
2 version:
layers_q = select([T2.id]).where(T2.box.op('&&')(box))
chunks = select([T1.id]).where(T1.arr.overlap(func.array(layers_q)))
I added func.array() for cast to array but result is not correct:
SELECT T1.id
FROM T1, (SELECT T2.id AS id
FROM T2
WHERE T2.box && %(box_1)s)
WHERE T1.arr && array((SELECT T2.id
FROM T2
WHERE T2.box && %(box_1)s))
There you can see what i have duplicate in FROM clause. How did it correctly?
I find solution!
func.array(select([T2.id]).where(T2.box.op('&&')(box)).as_scalar())
After added as_scalar() all be good, beacause in my select all ids need have in one array.
I have a table A, B, C which has many columns. Now I would like to join with some case condition, but the following query failed
SELECT
QTY AS QUANTITY,
IS_UT AS IS_UT,
G_ACC_ID AS G_ACC_ID,
CR_AND_F_ID AS CR_AND_F_ID,
TRNG_ENRMT_ID AS TRNG_ENRMT_ID
TRAINING_COURSE_ID AS TRAINING_COURSE_ID
(CASE WHEN A.CR_AND_F_ID IS NULL OR A.CR_AND_F_ID = 0 THEN
SELECT
A.CR_AND_F_ID AS CR_AND_F_ID,
A.IS_UT AS IS_UT,
A.G_ACC_ID AS G_ACC_ID,
A.CR_AND_F_ID AS CR_AND_F_ID,
A.TRNG_ENRMT_ID AS TRNG_ENRMT_ID,
t4.TRAINING_COURSE_ID AS TRAINING_COURSE_ID
FROM T_E_P_LD
LEFT OUTER JOIN T_TRAINING t2
ON t2.TRAINING_ID = A.TRNG_ENRMT_ID
LEFT OUTER JOIN TR_ENROL t3
ON t2.TR_ID = t3.TR_ID
LEFT OUTER JOIN TR_CLASS t4
ON t3.TR_CLASS_ID = t4.TR_CLASS_ID
ELSE A.CR_AND_F_ID
END) AS TEST
FROM T_E_P_LD
I also tried to write Inline view but that too failed.
Please let me know how to approach.
Thanks.
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
}
)