This question already has answers here:
Linq to Entity Join table with multiple OR conditions
(3 answers)
LEFT OUTER JOIN in LINQ
(24 answers)
Closed 6 months ago.
I have the following SQL query that has left join with tow parameters
select val1,val2 from tbl1
left join tbl2 on (tbl1.ID = tbl2.ForKey1 or tbl1.id= tbl2.ForKey2)
What is the equivalent Linq query to it that retrieve the same result ?
You can use documented Collection selector references outer in a non-where case
var query =
from tbl1 in ctx.tbl1
from tbl2 in ctx.tbl2
.Where(tb2 => tbl1.ID == tbl2.ForKey1 || tbl1.id == tbl2.ForKey2)
.DefaultIfEmpty()
select new
{
tbl1,
tbl2
};
Related
can anybody help me to convert this SQL query to LINQ code in MVC? I need to return a list. The DB context entity is: _dbContext.
select distinct table1.AIG_ID, table1.GMT_NAME, table1.AIG_Number
from table1 left join table2 on table1.AIG_ID = table2.AIG_ID**
var data=(from item in db.table1 join
item1 in db.table2 on item.AIG_ID equals item1.AIG_ID
select new {item.AIG_ID ,item.GMT_NAME ,item.AIG_Number }).GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault()).ToList();
I write this part for your distinct in sql
GroupBy(a=>a.AIG_ID).select(a=>a.FirstOrDefault())
This question already has answers here:
Oracle syntax left joins three or more tables
(2 answers)
Closed 7 years ago.
I have some tables in Oracle.
Table_1 contains :
id|name
1|boy
2|roy
Table_2 contains :
id|value
1|90
2|100
Table_3 contains :
id|class
1|A
2|B
I want to join all three tables.
Query_1 :
SELECT A.ID,A.NAME,B.VALUE,C.CLASS
FROM
TABLE_1 A, TABLE_2 B, TABLE_3 C
WHERE
A.ID=B.ID(+) AND
B.ID=C.ID(+)
OR
Query_2 :
SELECT A.ID,A.NAME,B.VALUE,C.CLASS
FROM
TABLE_1 A, TABLE_2 B, TABLE_3 C
WHERE
A.ID=B.ID(+) AND
A.ID=C.ID(+)
Which one is true?
When you need an Outer Join:
Your query will include two or more tables where there may not exist a match for every record. Examples will explain best. If you have two tables: "Parent", and "Work". If you want to ask the question: What contacts do we have within our parent list within local employers an inner join works fine:
select w.employer, w.phone, w.address,p.name, p.phone
from work w, parent p
where w.person_id = p.person_id
If the question is where do our parents work -- well some may be unemployed and have no match in the "work" table -- now you need an outer join. in Oracle SQL:
select p.name, p.phone, w.employer, w.phone, w.address,
from work w, parent p
where p.person_id = w.person_id(+)
The (+) always goes on the side where we need to "synthetically" add a missing id.
This question already has answers here:
Why use a JOIN clause versus a WHERE condition?
(11 answers)
Closed 7 years ago.
I'm little question... I think this is only a semantic question...
Can anyone explain me real diff on these two query? If there is one...
// #1 - Query with join
SELECT DISTINCT cli.COD_CLIENTE
FROM CLIENTI cli
JOIN BANCHE b
ON b.COD_BANCA = cli.COD_BANCA
JOIN SOA_CONO_VISIB cv
ON cv.COD_SOCIETA = b.SOCIETA_SOA
AND cv.FLAG_ENTITA_OPER = 1
AND cv.COD_MACRO_CLASSE <> 'RFE'
AND cv.DATA_FINE_OPERATIVITA IS NULL
WHERE cv.COD_SOCIETA = '01'
AND cv.COD_ENTITA = '00008';
// #2 - Query with implicit join
SELECT DISTINCT cli.COD_CLIENTE
FROM CLIENTI cli, BANCHE b, SOA_CONO_VISIB cv
WHERE cv.COD_SOCIETA = '01'
AND cv.COD_ENTITA = '00008'
AND b.COD_BANCA = cli.COD_BANCA
AND cv.COD_SOCIETA = b.SOCIETA_SOA
AND cv.FLAG_ENTITA_OPER = 1
AND cv.COD_MACRO_CLASSE <> 'RFE'
AND cv.DATA_FINE_OPERATIVITA IS NULL;
They are exactly the same, with implicit join you have to "manually" do references between tables and, with query that select from many tables, this could be an excessive work.
Here another question like yours, maybe you could find other information.
http://www.stackoverflow.com/questions/44917/explicit-vs-implicit-sql-joins
This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
Closed 8 years ago.
I am new to Oracle SQL and need some help with the writing the following query. Any guidance would be greatly appreciated. I am not sure I am going down the correct path with this or not.
I have two tables t1 and t2. t1 contains four fields f1...f4. t2 contains the same four fields. I need to update t1.f1 and t1.f4 with the values from t2.f1 and t2.f4, respectively, where t1.f2 = t2.f2 and t1.f3 = t2.f3. If there is a row that does not match these conditions, The row should not be updated.
Am I on the right path or completely lost?
UPDATE t1
SET (t1.f1=t2.f1,
t1.f4=t2.f4)
FROM t1
INNER JOIN (SELECT t2.f1, t2.f4 FROM t2)
ON t1.f2=t2.f2 AND t1.f3=t2.f3
WHERE EXISTS (SELECT t2.f1, t2.f4
FROM t2
WHERE t1.f2=t2.f2 AND t1.f3=t2.f3);
Might be easier if you use a MERGE statement. Something like:
MERGE INTO t1
USING
(SELECT f1,f2,f3,f4 FROM t2) t2
ON
(t1.f2=t2.f2
AND t1.f3 = t2.f3)
WHEN MATCHED THEN
update set t1.f1 = t2.f1,
t1.f4 = t2.f4;
If you are using Oracle version 9i or above 'MERGE' is the best way to do it.
Or you can rewrite your above query like this
UPDATE table1 t1
SET (t1.f1,t1.f2) = (
select t2.f1,t2.f4 from table2 t2
where t1.f2 = t2.f2 and t1.f3 = t2.f3)
WHERE EXISTS (SELECT 1
FROM table2 t2
WHERE t1.f2=t2.f2 AND t1.f3=t2.f3);
This question already has answers here:
C# Null coalesce with LINQ
(4 answers)
Closed 4 years ago.
This is my SQL query:
select
S.student_No,
coalesce(P.Name,'0'),
P.Surname
from
Person as P
join
Student as S
on P.Id = S.Person_Id
I want to convert it LINQ , i did it except coalesce function,
from P in cbu.PERSON
join S in cbu.STUDENT on P.ID equals S.PERSON_ID
select new
{
S.Stundent_No,
P.Name,
P.Surname,
};
how can I use coalesce in this linq query
P.Name ?? "0". C# has the coalesce operator built-in. Even if you didn't know that, you could use the ?: operator.
The join is not necessary, btw. You can just write P.Student.Stundent_No (or whatever the properties are called).