Join two tables using Entity Framework - entity-framework

Table details:
Login:
Loginid username
-------------------
1 a
2 b
3 c
4 d
5 e
Personal:
Loginid fname lname
------------------------
1 nitin kumar
2 pravin kumar
I want result like this:
Loginid username fname lname
--------------------------------
1 a nitin kumar
2 b pravin kumar
3 c
4 d
5 e
but when I used join
var balance = (from a in dbContext.login
join c in dbContext.Personal on a.loginid equals c.loginid
select new { a, c }).ToList();
result shows only two rows
How to do it ?
Thanks in advance

You can try as shown below.
var balance = (from a in dbContext.login
join c in dbContext.Personal on a.loginid equals c.loginid into lg
from x in lg.DefaultIfEmpty()
select new { a.Loginid,a.username,x.fname,x.lname
}).ToList();

Use a left join instead of join.
For example:
http://www.devcurry.com/2011/01/linq-left-join-example-in-c.html

Related

How can I do an IF ELSE statement on POSTGRES JOIN query without joining more than once?

I would like to join table_b on table_a conditionally where it first tries to join on animal_name, then person_name, then city_name, and finally, it doesn't join. However, if a match is found on animal_name, I don't want to double join on person_name. Here is the example:
table_a:
[id, animal_name, person_name, city_name]
1 dog tom ny
2 cat joe sf
3 frog ron la
4 duck ben ri
table_b:
[name, noise]
dog woof
joe hello
ny honk
I'd like to something like this:
SELECT * FROM table_a
IF
LEFT JOIN table_b on table_b.animal_name = table_a.name
ELSE IF
LEFT JOIN table_b on table_b.person_name = table_a.name
ELSE IF
LEFT JOIN table_b on table_b.city_name = table_a.name
And the result should look like this
[id, animal_name, person_name, city_name, noise]
1 dog tom ny woof
2 cat joe sf hello
3 frog ron la honk
Note, your example doesn't make sense:
It should be la instead of ny to get the desired output.
The joins in your attempt have table_a and table_b reversed.
This should do it:
SELECT
a.*,
COALESCE(b1.noise, b2.noise, b3.noise) AS noise
FROM table_a AS a
LEFT JOIN table_b AS b1 ON b1.name = a.animal_name
LEFT JOIN table_b AS b2 ON b2.name = a.person_name
LEFT JOIN table_b AS b3 ON b3.name = a.city_name
WHERE COALESCE(b1, b2, b3) IS NOT NULL
ORDER BY a.id;
You can try something like this:
LEFT JOIN table_b ON (table_a.animal_name = table_b.name) OR (table_a.person_name = table_b.name) OR (table_a.city_name = table_b.name)

C# - Which is syntax in LINQ to EF for this query?

I have two table
Teachers(**int IDT**, int mat, String name ) and Courses(String name, **int IDT**). IDT is FKey in Courses and PKey in Teachers.
Then the teacher cant are in more that 3 courses. My query work fine in sql. My question is, 'How write this in LINQ to EF'?
select p.name, p.mat, p.IDT, count(c.IDT) from Teachers p
left join courses c on c.IDT = p.IDT
group by p.name, p.mat, p.IDT
having count(c.IDT) <3
You can try as shown below.
from p in context.Teachers
join c in context.courses on c.IDT equals p.IDT into j1
from j2 in j1.DefaultIfEmpty()
group j2 by new { p.name, p.mat, p.IDT } into grouped
let theCount = grouped.Sum(e => e != null ? 1 : 0)
where theCount < 3
select new { Name = grouped.Key.name, Mat= grouped.Key.mat,
Idt=grouped.Key.IDT,Count = theCount }

Limit for inner Join Table

I have a scenario where I am joining three tables and getting the results.
My problem is i have apply limit for joined table.
Take below example, i have three tables 1) books and 2) Customer 3)author. I need to find list of books sold today with author and customer name however i just need last nth customers not all by passing books Id
Books Customer Authors
--------------- ---------------------- -------------
Id Name AID Id BID Name Date AID Name
1 1 1 ABC 1 A1
2 2 1 CED 2 A2
3 3 2 DFG
How we can achieve this?
You are looking for LATERAL.
Sample:
SELECT B.Id, C.Name
FROM Books B,
LATERAL (SELECT * FROM Customer WHERE B.ID=C.BID ORDER BY ID DESC LIMIT N) C
WHERE B.ID = ANY(ids)
AND Date=Current_date

How can I create a view from two tables to make a report in Crystal Reports?

I have two tables patient_data and patient_test.
patient_data:
lab_no p_name
1 p1
2 p2
3 p3
4 p4
patient_test:
lab_no test_name
1 t1
1 t2
1 t3
2 t1
2 t2
3 t2
4 t1
4 t3
When I try this code:
cmd.CommandText = "create view reg as SELECT patient_data.lab_no,patient_data.p_name,patient_test.test_name,patient_test.price from patient_data INNER JOIN patient_test on patient_data.lab_no = patient_test.lab_no where patient_data.lab_no ='" & TextBox2.Text & "'"
It returns an error and I don't know what the problem is.
Can you help me in this?
Thank you.
Are you sure you need to create a view? cmd.CommandText = "SELECT patient_data.lab_no,patient_data.p_name,patient_test.test_name,patient_test.price from patient_data INNER JOIN patient_test on patient_data.lab_no = patient_test.lab_no where patient_data.lab_no =" & TextBox2.Text

How to determine whether a value exists in a junction table and return zero or one?

I am using SQL Server 2008 R2
I am trying to write a single query that will return only exactly what I need. I will drop in a MovieID and get back a list of ALL genres. If the movie represents a specific genre (has an associated record in the junction table), the Checked value will be 1. If not, then 0.
My result set should look like this:
GenreID Genre Checked
1 ABC 0
2 DEF 1
3 HIJ 0
4 KLM 1
My First table is named Genres. It looks like this:
GenreID Genre
1 ABC
2 DEF
3 HIJ
4 KLM
My second table is named Movies. It looks like this:
MovieID Title
1 Blah
2 Foo
3 Carpe
4 Diem
My third table is a junction table named Movies_Genres. It looks like this:
MovieID GenreID
1 2
1 1
1 4
2 1
2 3
3 4
4 1
I would normally, do a couple of queries and a couple of loops to handle this, but I want to really just make the database do the work here. How do I tweak my query so that I can get the resultset that I need with just a single query?
Here's the starting query:
SELECT GenreID,
Genre
FROM Genres
Thanks in advance for your help!!!
SELECT g.GenreID, g.Genre, Checked = CASE WHEN EXISTS
(SELECT 1 FROM dbo.Movies_Genres AS mg
INNER JOIN dbo.Movies AS m
ON mg.MovieID = m.MovieID
WHERE mg.GenreID = g.GenreID
AND m.MovieID = #MovieID) THEN 1 ELSE 0 END
FROM dbo.Genres AS g
ORDER BY g.GenreID;
If there is a unique constraint or primary key on dbo.Movies_Genres(MovieID, GenreID) then this can be simply:
SELECT g.GenreID, g.Genre, Checked = COUNT(mg.GenreID)
FROM dbo.Genres AS g
LEFT OUTER JOIN dbo.Movies_Genres AS mg
ON g.GenreID = mg.GenreID
AND mg.MovieID = #MovieID
GROUP BY g.GenreID, g.Genre;
...since the count for any genre can only be 0 or 1 given a single #MovieID.
Pretty straight forward using CASE;
SELECT DISTINCT g.GenreID, g.Genre,
CASE WHEN mg.MovieID IS NULL THEN 0 ELSE 1 END Checked
FROM Genres g
LEFT JOIN Movies_Genres mg
ON g.GenreID=mg.GenreID
AND mg.MovieId=#MovieID;
Demo here.
Edit: If entries are guaranteed to be unique in Movies_Genres, you could choose to drop the DISTINCT.
The #MovieID is the movie, you want to filter by.
SELECT Genres.GenreID,
Genres.Genre,
CASE WHEN (Movies_Genres.GenreID IS NULL)
THEN 0
ELSE 1
END AS Checked
FROM Genres LEFT JOIN
Movies_Genres ON Movies_Genres.GenreID = Genres.GenreID AND
MovieID = #MovieID