This question already has an answer here:
Linq to SQL - Group By and Count
(1 answer)
Closed 6 years ago.
I need to show the company name with each number of employees (count of persons).
var result2 = (from c in NoOfEntities.listOfCompanies
join s in NoOfEntities.listOfStores on c.Id equals s.CompanyId
join p in NoOfEntities.listOfPersons on s.Id equals p.StoreId
group c by c.Name into newGroup
select newGroup );
I need to add the number of employees but I don`t have any idea how to add that counter.
var result2 = (from c in NoOfEntities.listOfCompanies
join s in NoOfEntities.listOfStores on c.Id equals s.CompanyId
join p in NoOfEntities.listOfPersons on s.Id equals p.StoreId
group p by c.Name into newGroup
select new
{
CompanyName = newGroup.Key,
NumberOfEmployees = newGroup.Count()
});
Related
How to translate an SQL query to LINQ
SELECT DISTINCT q.Id
FROM Questions AS q
JOIN TagsQuestions AS tq
ON q.Id = tq.QuestionForeignKey
JOIN Tags AS t
ON t.Id = tq.TagForeignKey
WHERE t.Name IN ('C#', '.Net')
GROUP BY q.Id
HAVING COUNT(*) = 2
Firstly, You should note use DISTINCT q.Id because it's already GROUP BY q.Id.
SELECT q.Id
FROM Questions AS q
JOIN TagsQuestions AS tq ON q.Id = tq.QuestionForeignKey
JOIN Tags AS t ON t.Id = tq.TagForeignKey
WHERE t.Name IN ('C#', '.Net')
GROUP BY q.Id
HAVING COUNT(*) = 2
Secondly, You can transfer from sql to linq syntax like below
var TagNames = new[] {
"C#",
".Net"
};
var result = from q in Questions
join tq in TagsQuestions on q.Id equals tq.QuestionForeignKey
join t in Tags on t.Id equals tq.TagForeignKey
where TagNames.Contains(t.Name)
group q by q.Id into g
where g.Count() = 2
select g.Key;
I'm trying to join few tables using LINQ and retrieve a data set. But my problem is when I group the tables, I cannot access the fields of the non grouped tables in the LINQ query. here is my sample code block. Please help
var query = from sb in db.Surfboards
join csb in db.CustomerSurfBoards on sb.SurfBoardID equals csb.SurfBoardID
join c in db.Customers on csb.CustomerID equals c.CustomerID
where c.IsActive
group new { sb,csb} by new {sb.ID, csb.ComponentId} into g
select new ReportModel()
{
ReceivedDate = g.First().Name,
Number = c.First().Number <------- this cannot be accessed
}
what I'm trying to achieve is something like this
select sb.Id, max(c.Number), cbs.Id from Surfboards as sb
inner join CustomerSurfBoards as cbs on sb.SurfBoardID = csb.SurfBoardID
inner join Customers as c on csb.CustomerID = c.CustomerID
group by sb.Id, csb.ComponentId
Try this:
var query = from sb in db.Surfboards
join csb in db.CustomerSurfBoards on sb.SurfBoardID equals csb.SurfBoardID
join c in db.Customers on csb.CustomerID equals c.CustomerID
where c.IsActive
select new {sb.id, c.number, cbs.componentid} into tmp
from t in tmp
group t by new {t.ID, t.ComponentId} into g
select new
{
g.Key.id, g.Key.componentid, number = g.Select(n=>n.number).OrderByDescending().FirstOrDefault()
}
Please is somebody able to help me convert the following SQL query into LINQ
select p.Description,SUM(s.TotalArea) as TotalArea from Stands s
inner join ContractProducts cp on s.Id = cp.StandId
inner join Products p on cp.ProductId = p.Id
where s.EventId = 1
group by p.Description
Thanks in advance
Maybe something like this:
var result= (
from s in db.Stands
join cp in db.ContractProducts
on s.Id equals cp.StandId
join p in db.Products
on cp.ProductId equals p.Id
where s.EventId == 1
group p by p.Description into g
select new
{
Description=g.Key,
TotalArea = g.Sum (x =>x.TotalArea)
}
).ToList();
Where db is the linqdatacontext
Can someone please help me convert this SQL to linq or lambda c#
select
count(s.ClassId) [StudentInClass], c.Name [Class], t.Name [teacher]
from
[dbo].[Students] s
inner join
class c on s.ClassId = c.Id
inner join Teacher t
on t.Id = c.TeacherId
group by
s.ClassId, c.Name, t.Name
so far this is what I have, and i am messing it up. I want to achieve the same results as in my sql query
SchoolEntities db = new SchoolEntities();
var StudentsByCourseId = from s in db.Students
join c in db.Classes on s.ClassId equals c.Id
join t in db.Teachers on c.TeacherId equals t.Id
group c by s.ClassId
into g
select g;
in SQL this is what my reults look like, it counts the students in a class by the teacher
StudentCount Class Teacher
1 Geography Teacher1
1 Biology Teacher1
2 Maths Teacher2
You can use an anonymous class to group by multiple properties.
var StudentsByCourseId = from s in db.Students
join c in db.Classes on s.ClassId equals c.Id
join t in db.Teachers on c.TeacherId equals t.Id
group s by new { s.ClassId, Class = c.Name, Teacher = t.Name }
into g
select new
{
StudentInClass = g.Count(),
g.Key.Class,
g.Key.Teacher,
};
I am having problems with a SQL query, i need a list of consumers having purchased at least 3 different products whose suppliers are from a certain city (Lets say new york).
Tabels Columns:
Tb_Consumer..........Con_ID(PK), Name, City
Tb_Supplier.............Supp_ID(PK), Name, City
Tb_Transactions.....Tran_ID(PK), Supp_ID(FK), Con_ID(FK), PROD_ID(FK)
Tb_Products............Prod(ID(PK), Name
What i have so far:
var query8Result = (from c in context.Tb_Consumer
join t in context.Tb_Transactions on c.Con_ID equals t.Con_ID
join s in context.Tb_Supplier on t.Supp_ID equals s.Supp_ID
join p in context.Tb_Product on t.Prod_ID equals p.Prod_ID
where s.City == "New York"
select new { Name = c.Name }).Distinct();
I think you need a few Group-by's
var query8Result = (from c in context.Tb_Consumer
join t in context.Tb_Transactions on c.Con_ID equals t.Con_ID
join s in context.Tb_Supplier on t.Supp_ID equals s.Supp_ID
join p in context.Tb_Product on t.Prod_ID equals p.Prod_ID
where s.City == "New York"
group c by new { c.Name, t.Prod_ID } into customerProducts
group customerProducts by new { customerProducts.Key.Name } into customers
where customers.Count() > 3
select g.Key);
Sorry if this isn't 100% correct - It's a little hard to test this...