I want to join 2 tables and get some data out of it using LINQ. here are the 2 ways in which i can do this
1.
var orders = from order in db.Order
from user in db.User
where order.UserId == user.UserId
select order;
2.
var result = from order in db.Order
join user in db.user
on order.UserId equals user.userId
select order
are these queries one and the same? are they different in any way?
Both are same. First syntax is implicit, second one is explicit join syntax.
Refer to wikipedia link for both type of syntax.
Related
i am writing a LINQ query but i have no idea of LINQ but i know how to write sql Query , someone please help and give me exact answer. thanks in advance
select count(t.taskid),
t.userid
from task t,
userr u
where u.userid = t.userid
group by t.userid;
You're close, but to make the aliases in LINQ, you have to switch just a few things around from the SQL way.
The select always goes at the end.
Aliases look like assignment statements.
I didn't test this, also you should use the LINQ join method. This is just to illustrate SQL-LINQ one-for-one.
var items=
from t=task,
u=user
where u.userid = t.userid
group by t.userid;
select count(t.taskid), t.userid <=== you can't do this, BTW, get rid of t.userid
Hello i am totally new to Linq. I need to convert the following query to Linq and having a pretty hard time. Almost 3 hrs spent still unable to figure out. Most of the functions/methods in Sql like Distinct, Not in etc are missing in Linq. Even if they are available i am unable to figure out how to use them. Are there any alternative Methods/Functions in Linq with different names that i should be using or they don't even exist in Linq and i need to use a different approach ? I would be really helpful if someone could help me in converting the following query to Linq.
SQL Query
Select count(distinct(UserID)) from dbo.DeansStudents
inner join dbo.UserBarSession on DeansStudents.UserBarSessionID = UserBarSession.ID
inner join dbo.Users on users.ID = UserBarSession.UserID
where UserBarSessionID not in (
Select b.ID from dbo.DeansStudents,dbo.Users
left join dbo.Answers on answers.Student=users.ID
left join dbo.UserBarSession b on Answers.Student = b.UserID
where AnswerDate between b.StartDate and b.EndDate
and AnswerDate between 7/10/2011 and 3/12/2018
and UserBarSessionID = b.ID and DeanID= 12296 group by Answers.Student,users.FirstName,users.LastName,b.ID) and DeanID =12296
Query converted so far to LINQ
From my past couple of days into LINQ i managed to converted the first part of the Sql Query to LINQ. I am unable to continue with the second part. From "Select b.id........ "
var query = from deansStudent in dbo.DeansStudents
join userBarSession in dbo.UserBarSession
on deansStudent.UserBarSessionId equals userBarSession.Id
join users in dbo.Users
on userBarSession.UserId equals users.Id
//Need continuation from here
Most of the functions/methods in Sql like Distinct, Not in etc are missing in Linq
Distinct() is part of Linq, and NOT IN can be accomplished with the Except() Linq extension method.
With this answer, you should be able to finish your query.
I want to know which one is better for performance:
//Logical
var query = from i in db.Item
from c in db.Category
where i.FK_IdCategory == c.IdCategory
Select new{i.name, c.name};
or
//Join
var query2 = from i in db.Item
join c in db.Category
on c.ID equals i.FK_IdCategory
Select new{i.name, c.name};
Performance of the two queries really depends on which LINQ provider and which RDBMS you're using. Assuming SQL Server, the first would generate the following query:
select i.name, c.name
from Item i, Category c
where i.FK_idCategory = c.IdCategory
Whereas the second would generate:
select i.name, c.name
from Item i
inner join Category c
on i.FK_idCategory = c.IdCategory
Which operate exactly the same in SQL Server as is explained in: Explicit vs implicit SQL joins
This depends on the ORM you're using and how intelligent it is at optimizing your queries for your backend.
Entity Framework can generate some pretty awful SQL if you don't do your linq perfectly, so I'd assume query2 is better.
The only way for you to know for sure would be to inspect the SQL being generated by the two queries.
Eyeballing it, it looks like query1 would result in both tables being pulled in their entirety and then being filtered against each other in your application, while query2 will for sure generate an INNER JOIN in the query, which will let SQL Server do what it does best - set logic.
Is that FK_IdCategory field a member of an actual foreign key index on that table? If not, make it so (and include the name column as an included column in the index) and your query will be very highly performant.
With linq2Sql or EntityFramework, you would probably do something like this:
var query = from i in db.Item
select new {i.name, i.Category.Name}
This will generate a proper SQL inner join.
I do assume that there is a foreign key relation between Item and Category defined.
I have a main table called M1 which consists of Ids of three individual tables T1,T2 and T3.
I need to join 3 tables onto M1 using their Ids and display their Names
For this I am using the following query:
var query= (from i in dbContext.M1
join j in dbContext.T1 on i.Mt1_id equals j.Mt1_id
join l in dbContext.T2 on i.Mt2_id equals l.Mt2_id
join s in dbContext.T3 on i.Mt3_id equals s.Mt3_id
where i.Mid >= 1
select new
{
a=j.name,
b=l.name,
c=s.name
}).ToArray();
I have used this way, but I am getting an error stating that "Type inference failed in call to Join"
Could any one please tell me where I went wrong?
Please check Datatype of columns to avoid "Type inference failed in call to Join" exception
In NHibernate HQL you can select multiple entities for a given query like this example.
var query = session.CreateQuery("select c,k from Cat as c join c.Kittens as k");
Obviously the real world situation has more complexity but that is the basics. Is there a way to do this in a Criteria query?
You need to use JOIN FETCH.
HQL would be this -
FROM Cat C JOIN FETCH C.Kittens
var catsWithKittens = session.createCriteria()
.SetFetchmode("Kittens", Fetchmode.Eager)
.List();