我坚持这个问题。我希望得到处于“开发人员”角色的用户,他们既没有被分配任何任务,也没有被分配3个或更少的被标记为“活动”或“测试”的任务。
我不知道如何发表一个声明,其中我获得状态为“活动,测试”的所有任务,并将其总结为我的计数声明。
//here i specific role for users
var role = unitOfWork.RoleRepository._context.Roles.SingleOrDefault(m => m.Name == "Developer");
var query = (from u in users
where u.Roles.Any(r => r.RoleId == role.Id)
from t in u.ProjectTasks.Where(x => x.Users.Any(user => user.Id == u.Id)).DefaultIfEmpty()
//here I am able to get those users without assigned tasks but I dont know how to make the second condition with 3 or less tasks
where ((u.ProjectTasks.Count() == 0) || u.ProjectTasks.Any(z => z.Status == Status.Active || z.Status == Status.Testing))
select new { User = u } into Users
group Users by Users.User).ToList();
答案 0 :(得分:1)
可能会调用LINQ Count
函数:
var query = (from u in users
where u.Roles.Any(r => r.RoleId == role.Id)
from t in u.ProjectTasks.Where(x => x.Users.Any(user => user.Id == u.Id)).DefaultIfEmpty()
where ((u.ProjectTasks.Count() == 0) || u.ProjectTasks.Count(z => z.Status == Status.Active || z.Status == Status.Testing) <= 3)
select new { User = u } into Users
group Usersby Users.User).ToList();
答案 1 :(得分:1)
我使用DataTable模拟数据库以显示它是如何完成的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable AspNetRoles = new DataTable();
AspNetRoles.Columns.Add("Id", typeof(int));
AspNetRoles.Columns.Add("Name", typeof(string));
AspNetRoles.Rows.Add(new object[] { 123, "Developer" });
AspNetRoles.Rows.Add(new object[] { 456, "Non Developer" });
DataTable AspNetUserRoles = new DataTable();
AspNetUserRoles.Columns.Add("UserId", typeof(int));
AspNetUserRoles.Columns.Add("RoleId", typeof(int));
AspNetUserRoles.Rows.Add(new object[] { 100, 123 });
AspNetUserRoles.Rows.Add(new object[] { 200, 456 });
AspNetUserRoles.Rows.Add(new object[] { 300, 123 });
AspNetUserRoles.Rows.Add(new object[] { 400, 456 });
DataTable AspNetUsers = new DataTable();
AspNetUsers.Columns.Add("Id", typeof(int));
AspNetUsers.Rows.Add(new object[] { 100 });
AspNetUsers.Rows.Add(new object[] { 200 });
AspNetUsers.Rows.Add(new object[] { 300 });
AspNetUsers.Rows.Add(new object[] { 400 });
DataTable UserToTask = new DataTable();
UserToTask.Columns.Add("UserId", typeof(int));
UserToTask.Columns.Add("TaskId", typeof(int));
UserToTask.Rows.Add(new object[] { 100, 1000 });
UserToTask.Rows.Add(new object[] { 100, 1001 });
UserToTask.Rows.Add(new object[] { 100, 1002 });
UserToTask.Rows.Add(new object[] { 200, 1001 });
UserToTask.Rows.Add(new object[] { 200, 1004 });
UserToTask.Rows.Add(new object[] { 200, 1006 });
UserToTask.Rows.Add(new object[] { 300, 1005 });
UserToTask.Rows.Add(new object[] { 300, 1006 });
UserToTask.Rows.Add(new object[] { 400, 1007 });
UserToTask.Rows.Add(new object[] { 400, 1008 });
DataTable ProjectTasks = new DataTable();
ProjectTasks.Columns.Add("Id", typeof(int));
ProjectTasks.Columns.Add("Status", typeof(string));
ProjectTasks.Rows.Add(new object[] { 1000, "Active" });
ProjectTasks.Rows.Add(new object[] { 1001, "Testing" });
ProjectTasks.Rows.Add(new object[] { 1002, "Idle" });
ProjectTasks.Rows.Add(new object[] { 1003, "Active" });
ProjectTasks.Rows.Add(new object[] { 1004, "Testing" });
ProjectTasks.Rows.Add(new object[] { 1005, "Idle" });
ProjectTasks.Rows.Add(new object[] { 1006, "Active" });
ProjectTasks.Rows.Add(new object[] { 1007, "Testing" });
ProjectTasks.Rows.Add(new object[] { 1008, "Idle" });
var results = (from aspNetUser in AspNetUsers.AsEnumerable()
join task in UserToTask.AsEnumerable() on aspNetUser.Field<int>("Id") equals task.Field<int>("UserId")
join proj in ProjectTasks.AsEnumerable() on task.Field<int>("TaskId") equals proj.Field<int>("Id")
join aspNetUserRole in AspNetUserRoles.AsEnumerable() on aspNetUser.Field<int>("Id") equals aspNetUserRole.Field<int>("UserId")
join aspNetRole in AspNetRoles.AsEnumerable() on aspNetUserRole.Field<int>("RoleId") equals aspNetRole.Field<int>("Id")
select new { aspNetUser = aspNetUser, task = task, proj = proj, aspNetUserRole = aspNetUserRole, aspNetRole = aspNetRole }).ToList();
var finalResults = results.GroupBy(x => x.aspNetUser.Field<int>("Id"))
.Where(x => (x.Select(y => y.task).Count() == 0) || (x.Where(y => (y.proj.Field<string>("Status") == "Active") || (y.proj.Field<string>("Status") == "Testing"))).Count() <= 3).ToList();
}
}
}