模型类
public class tstsStates
{
public string Name { get; set; }
public string title{ get; set; }
public int jcount { get; set; }
public int DCount { get; set; }
public Guid JobId { get; set; }
public Guid PId { get; set; }
public Guid JId { get; set; }
public DateTime Date { get; set; }
}
我的Lambda查询
List<model> list= _context.Table
.Include(x => x.Table1)
.Include(x => x.Table2)
.Include(x => x.Table2)
.Where(x => Ids.Contains(x.Table4.Id))
.Select(x => new model
{
Name = x.table.FirstName + "" + x.table.LastName,
Position = x.table.Title,
JobId = x.table.Id,
id= x.Id,
rId= x.Id,
Date = x.Date
}).ToList(); //Till here query works absolutely fine.
// How i can group two columns and get the count.
我的问题:
1)我错过了按两列(PId,jbId)分组的逻辑,计算并将其分配回我的模型以将其返回到视图。
2)我错过了两列(pid,mydate)分组的逻辑,接受dCount并将其分配回我的模型以将其返回到视图。
我的要求
jcount:我需要从上面的IEnumerable中获取Distinct JobIds的数量?
dcount:我需要从IEnumerable上面算一下Distinct Date?
原因:
答案 0 :(得分:0)
据我所知,你想要显示JobsCount&amp;每个人的天数和每个人都有不同的标题集,在这种情况下试试这个: -
var result = tstPData.GroupBy(x => new { x.PersonnelId, x.Name , x.Position })
.Select(x => new
{
PersonnelId = x.Key.PersonnelId,
Name = x.Key.Name,
Position = x.Key.Position ,
JobCount = x.Count(),
DaysCount = x.Count()
});
最后,您可以枚举结果并分配给您的模型。
<强>更新强>
我已根据您的模型创建了自定义数据对象: -
List<PersonnelStats> persons = new List<PersonnelStats>
{
//Same Job for Alex but different date
new PersonnelStats { Name="Alex", Position="Dev", JobId= new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,12)},
new PersonnelStats { Name="Alex", Position="Dev", JobId= new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-5566-7788-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,15)},
//Two jobs for Mary
new PersonnelStats { Name="Mary", Position="App", JobId= new Guid("11229944-2356-7788-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-1567-6565-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,13)},
new PersonnelStats { Name="Mary", Position="App", JobId= new Guid("11229944-3445-7788-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-1567-6565-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,13)},
//Two jobs for Mark with 1 job being at different dates.
new PersonnelStats { Name="Mark", Position="Test", JobId= new Guid("11223344-7879-2344-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-2245-4343-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,17)},
new PersonnelStats { Name="Mark", Position="Test", JobId= new Guid("11223344-5522-2344-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-2245-4343-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,17)},
new PersonnelStats { Name="Mark", Position="Test", JobId= new Guid("11223344-5522-2344-99AA-BBCCDDEEFF00"), PersonnelId = new Guid("11223344-2245-4343-99AA-BBCCDDEEFF00"), Date= new DateTime(2015,03,18)},
};
这是给我所需输出的代码: -
var result = persons.GroupBy(x => new { x.PersonnelId, x.Name, x.Position })
.Select(x => new
{
PersonnelId = x.Key.PersonnelId,
Name = x.Key.Name,
Position = x.Key.Position,
JobCount = x.Select(z => z.JobId).Distinct().Count(),
DaysCount = x.Select(z => z.Date.Date).Distinct().Count()
});