简体   繁体   中英

Adding Count Column to LINQ query Based on Joined Table

So I have a SQL view that I've created that provides me what I need. Essentially it's a job position billeting system that shows how many positions have been authorized vs filled (or assigned).

SELECT Companies.Name AS Company, Grades.Name AS Grade, Series.Name
AS Series, Positions.Authorized, COUNT(People.PersonId) AS Assigned

FROM Companies INNER JOIN
     Positions ON Companies.Id = Positions.CompanyId INNER JOIN
     Series ON Positions.SeriesId = Series.Id INNER JOIN
     Grades ON Positions.GradeId = Grades.Id INNER JOIN
     People ON Positions.CompanyId = People.CompanyId AND
     Positions.SeriesId = People.SeriesId AND Positions.GradeId = People.GradeId

GROUP BY Companies.Name, Grades.Name, Series.Name, Positions.Authorized

授权的SQL查询

Now what I'd like to be able to do is recreate this in a LINQ query. I've almost got it where I need it; however, I can't figure out how to add the counted column at the end that's based on the People table.

Here's my current LINQ query:

var query = from a in db.Companies
            join b in db.Positions on a.Id equals b.CompanyId
            join c in db.Series on b.SeriesId equals c.Id
            join d in db.Grades on b.GradeId equals d.Id
            join e in db.People on new { b.CompanyId, b.SeriesId, b.GradeId } equals new { e.CompanyId, e.SeriesId, e.GradeId }
            group a by new { CompanyName = a.Name, GradeName = d.Name, SeriesName = c.Name, b.Authorized, e.PersonId } into f
            select new { Company = f.Key.CompanyName, Grade = f.Key.GradeName, Series = f.Key.SeriesName, f.Key.Authorized, Assigned = /* needs to be Count(People.PersonId) based on last join */ )};

Thanks in advance for any help you can provide!

Figured it out. The reason why it was posting multiple rows and not doing a proper count on the same row was because in my "group by" I added in "e.PersonId" when it should have simply been removed. I also had to add a few things to make it work on the front-end razor views since it's an anonymous type (this doesn't have anything to do with the original question, but thought I'd give reason to the changes). So the person who removed their answer, you were partially right, but the reason it wasn't working was because of the additional fieldin the group by:

dynamic query = (from a in db.Companies
                    join b in db.Positions on a.Id equals b.CompanyId
                    join c in db.Series on b.SeriesId equals c.Id
                    join d in db.Grades on b.GradeId equals d.Id
                    join e in db.People on new { b.CompanyId, b.SeriesId, b.GradeId } equals new { e.CompanyId, e.SeriesId, e.GradeId }
                    group a by new { CompanyName = a.Name, GradeName = d.Name, SeriesName = c.Name, b.Authorized } into f
                    select new { Company = f.Key.CompanyName, Grade = f.Key.GradeName, Series = f.Key.SeriesName, Authorized = f.Key.Authorized, Assigned = f.Count()}).AsEnumerable().Select(r => r.ToExpando());

And what it looks like on the page:

转换后的授权LINQ剃须刀页面样本

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM