i want to translate this SQL query :
SELECT FirstName,LastName, WaiterId, Count(*) AS compte
FROM Waiter
INNER JOIN Client on Waiter.Id = Client.WaiterId
GROUP BY WaiterId,lastname, firstname
ORDER BY compte DESC;
in entity framework.
I tried something like this :
var query = (from w in db.Waiter
join c in db.Client on w.Id equals c.WaiterId
group c by c.WaiterId into g
//orderby
select new
{
WaiterId = g.Key,
count = g.Count()
});
but my select don't work. I can't select FirstName and LastName and i don't even know if my count is good.
You need to include all the properties in the group by.
var query = (from w in db.Waiter
join c in db.Client on w.Id equals c.WaiterId
group c by new { c.FirstName, c.LastName, c.WaiterId} into g
orderby g.Count() descending
select new
{
FirstName = g.Key.FirstName,
LastName = g.Key.LastName,
WaiterId = g.Key.WaiterId,
count = g.Count()
});
Related
I have 3 table User,HumanCustomer,CompanyCustomer
I want to join between tables with LINQ but my query is not working and its returning null
Query:
var query = (from users in _ctx.Users
join hCustomer in _ctx.HumanCustomers on users.Id equals hCustomer.UserId
join cCustomer in _ctx.CompanyCustomers on users.Id equals cCustomer.UserId
select new
{
users.Id,
users.Mobile,
hCustomer.LastName,
hCustomer.Name,
cCustomer.CompanyName
});
foreach (var item in query)
{
AllCustomerViewModel allCustomer = new AllCustomerViewModel();
if (item.Name != null)
{
allCustomer.UserId = item.Id;
allCustomer.FullName = item.Name + item.LastName;
allCustomer.Mobile = item.Mobile;
Customer.Add(allCustomer);
}
}
The code does not enter foreach
I think the problem is using UserId twice because when I delete
join cCustomer in _ctx.CompanyCustomers on users.Id equals cCustomer.UserId
the query runs correctly.
Perhaps your issue is the dynamic variable you are attempting to iterate over.
You could try:
foreach (dynamic item in query)
Alternatively, you write the class to hold the values you are selecting out by updating the beginning of your query to:
var query = (from users in _ctx.Users
join hCustomer in _ctx.HumanCustomers on users.Id equals hCustomer.UserId
join cCustomer in _ctx.CompanyCustomers on users.Id equals cCustomer.UserId
select new MyClass
{
Id = users.Id,
Mobile = users.Mobile,
LastName = hCustomer.LastName,
Name = hCustomer.Name,
CompanyName = cCustomer.CompanyName
});
you can try this way..
var query = (from users in _ctx.Users
join hCustomer in _ctx.HumanCustomers on users.Id equals hCustomer.UserId
join cCustomer in _ctx.CompanyCustomers on users.Id equals cCustomer.UserId
select new
{
Id = users.Id,
Mobile = users.Mobile,
LastName = hCustomer.LastName,
Name = hCustomer.Name,
CompanyName = (cCustomer == null ? "" : cCustomer .CompanyName)
});
I'm having difficulty converting this sql query to a linq query. I'm trying to select all my orders with the order details attached to that order. In sql the query is correct. When I write it in linq is when everything goes haywire. Here is my code:
var result = from order in orders
join orderDetail in orderDetails
on order.OrderID equals orderDetail.OrderID
select new OrderVm
{
OrderId = order.OrderID,
OrderDetails = order.Order_Details.Select(x =>
new OrderDetailVm
{
OrderId = orderDetail.OrderID,
UnitPrice = orderDetail.UnitPrice,
Quantity = orderDetail.Quantity,
ProductId = orderDetail.ProductID
})
};
return result.ToList();
that produces this result
but in sql if I write
select
o.OrderID,
o.CustomerID,
od.UnitPrice,
od.Quantity,
od.productid,
p.ProductName
from Orders o
inner join [Order Details] od
on o.OrderID = od.OrderID
inner join Products p
on p.ProductID = od.ProductID
where o.OrderID = 10248
my expected out come is
It looks like from the way that you are using Order_Details in the nested object that the join already exists and that the join you are using is redundant.
var result = from order in orders
select new OrderVm
{
OrderId = order.OrderID,
OrderDetails = order.Order_Details.Select(x =>
new OrderDetailVm
{
OrderId = x.OrderID,
UnitPrice = x.UnitPrice,
Quantity = x.Quantity,
ProductId = x.ProductID
})
};
I have a sql query which will give me around 30 record but the same query once I changed to linq query its giving me thousands of record. I cant find the actual root cause of the problem can any one help me ...
sql query
select
DLId = p.Id,
TopicId = st.Id,
TopicName = at.Name,
PrimaryOrg = bo.BusinessUnit,
StatusId = ns.ID,
ModifiedBy = pa.LastName
from STopics st
join ATopics at on st.Id = at.Id
join Students p on st.StudentId = p.Id
join Sorgs sbu on at.BUorgID = sbu.BUOrgID
join BOrgs bo on sbu.BUOrgID = bo.ID
join Status ns on st.SID = ns.ID
join Students pa on st.NominatedBy = pa.Email
where p.IsActive = 1 and sbu.StudentID = 123 and sbu.IsActive = 1
and the linq query is
(from st in Context.STopics
join at in Context.ATopics on st.Id equals at.Id
join p in Context.Students on st.StudentId equals p.Id
join sbu in Context.Sorgs on at.BUorgID equals sbu.BUOrgID
join bo in Context.BOrgs on sbu.BUOrgID equals bo.ID
join ns in Context.Status on st.SID equals ns.ID
join pa in Context.Students on st.NominatedBy equals pa.Email
where p.IsActive==true && sbu.StudentID == 123 && sbu.IsActive == true
select new result()
{
DLId = p.Id,
TopicId = st.Id,
TopicName = at.Name,
PrimaryOrg = bo.BusinessUnit,
StatusId = ns.ID,
ModifiedBy = pa.LastName
})
(from st in Context.STopics
join at in Context.ATopics on st.Id equals at.Id
join p in Context.Students on new { st.StudentId, p.IsActive } equals new { p.Id , true}
join sbu in Context.Sorgs on new { sbu.BUorgID, sbu.IsActive,sbu.StudentID } equals new { at.BUorgID , true, 123}
join bo in Context.BOrgs on sbu.BUOrgID equals bo.ID
join ns in Context.Status on st.SID equals ns.ID
join pa in Context.Students on st.NominatedBy equals pa.Email
select new result()
{
DLId = p.Id,
TopicId = st.Id,
TopicName = at.Name,
PrimaryOrg = bo.BusinessUnit,
StatusId = ns.ID,
ModifiedBy = pa.LastName
})
Can you try this one, if you want to get exact issue, you can get how sql query is generated from the linq statements, linq query uses lots of inner query methodology
var query= your linqquery;
string sqlQuery=query.ToString();
you can review sqlQuery.
I recently ran into some SQL code that looks like this
SELECT
a.Id,
b.Id,
c.Id,
d.Id,
b.Name,
d.Occupation,
FROM TableA a
JOIN TableB b ON a.Id = b.Id
JOIN TableC c ON b.Id = c.Id AND b.Name = a.Name
JOIN tableD d ON c.Id = d.Id AND c.Occupation = d.Occupation
I've never used ANDs inside JOINs like this in a corresponding LINQ query (for context, I use the Entity Framework and LINQ for all of my queries). I usually put all my ANDs inside there where clause like this:
var query = from a in dbContext.TableA
join b in dbContext.TableB on a.Id equals b.Id
join c in dbContext.TableC on b.Id equals c.Id
join d in dbContext.TableD on c.Id equals d.Id
where b.Name = a.Name
where c.Occupation = d.Occupation
select new
{
AId = a.Id,
BId = b.Id,
CId = c.Id,
DId = d.Id,
BName = b.Name,
DOccupation = d.Occupation,
};
What technique can I use to get my LINQ query to match the SQL statement above?
You can join on multiple properties by constructing anonymous types from your properties like this:
var query = from a in dbContext.TableA
join b in dbContext.TableB on new { a.Id, a.Name } equals new { b.Id, b.Name }
join c in dbContext.TableC on b.Id equals c.Id
join d in dbContext.TableD on new { c.Id, c.Occupation } equals new { d.Id, d.Occupation }
select new
{
AId = a.Id,
BId = b.Id,
CId = c.Id,
DId = d.Id,
BName = b.Name,
DOccupation = d.Occupation,
};
Note that the name, order, and datatype of each property in the anonymous types need to be the same on both sides of the equals in order for this to work. Otherwise they would be two different anonymous types and would not be equatable.
I have three tables, Professors, ProfessorStudent, Student.
I want all Professors + How many Students each Professor have.
I can do this:
context.ProfessorSet.Include("Student")
context.ProfessorSet.Include("Student").ToList() will read all three tables.
But i dont wanna get Student table, I want that Linq just get "Professor Table"+ "Count(*) ProfessorStudent Group By StudentId".
Its possible?
I do using this:
var c = from tag in contexto.ProfessorSet
select new
{
Tag = tag,
Count = tag.Student.Count
};
But generate this SQL:
SELECT
C.Id,
C.Nome,
C.C1
FROM
(SELECT
A.Id,
A.Nome,
(SELECT
COUNT(0)
FROM ProfessorStudant AS B
WHERE A.Id = B.ProfessorId
) AS [C1]
FROM Professor AS A)
I want this:
Select A.Id, Count(0) from Professor A
inner join ProfessorStudent B on A.Id = B.ProfessorId
Group By A.Id
from p in context.ProfessorSet
from s in p.Student
group s by s.StudentId into g
select new
{
Professor = p,
StudentCounts = from sc in g
select new
{
StudentId = sc.Key,
Count = sc.Group.Count()
}
}