Select multiple entities in Criteria Query - c#

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();

Related

Sql query with count, inner join, groupby to LINQ

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

Alternative methods/functions in LINQ while converting from SQL to Linq

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.

Include method in LINQ is used for Left Join?

I'm using Entity Framework 6, DotConnect for Oracle and i have these 2 queries:
First one, using a simple join (LINQ and Output SQL):
LINQ:
var joinQuery = Db.Products
.Join(Db.Product_Categories.AsEnumerable(), p => p.ProductID,
pc => pc.CategoryID, (pc, p) => new { pc, p })
.ToList();
Output SQL:
SELECT * FROM Products
Second, using Include:
LINQ:
var includeQuery = Db.Products.Include("Product_Categories").ToList();
Output SQL:
SELECT * FROM Products
LEFT OUTER JOIN Product_Categories
ON Products.CategoryID = Product_Categories.CategoryID
I am in doubt if i can always use "Include" method for left joins. This method is not clear for my.
In the first example the join should not have .AsEnumerable() on the end of it. By doing that you are causing EF to go and get all the records from Product_Categories and then doing the join in memory which can be very inefficient as it doesn't use any kind of index.
The second option you have isn't pure LINQ. Include is an EF-specific extension method that is not available in other providers.
So if you want common LINQ you could use with other DB providers go with option 1. If you want simpler syntax and okay with being EF specific option 2 might be better.

LINQ Logical join VS inner join

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.

Do these 2 LINQ join queries achieve the same thing

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.

Categories

Resources