我的项目中有供应商模型和用户模型,每个供应商都有几个用户
供应商模型
public class SupplierRow
{
public Guid Id { get; set; }
public string FullName { get; set; }
public bool Subscribed { get; set; }
public bool Active { get; set; }
public int Visits { get; set; }
public bool AllUsersInactive { get; set; }
}
和用户模型
public class UserRow
{
public Guid Id { get; set; }
public string FullName { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public int Status { get; set; }
public int Role { get; set; }
public Guid SupplierId { get; set; }
public bool SupplierIsInactive { get; set; }
}
在供应商的控制器中,我检查每个供应商的所有用户是否都没有激活状态
public ActionResult Grid(bool? active)
{
var suppliers = Context.Suppliers.AsNoTracking()
.WhereIf(active != null, e => e.Active == active)
.Select(e => new SupplierRow
{
Id = e.Id,
FullName = e.FullName,
Active = e.Active,
Visits = e.Visits,
})
.ToList();
var supplierIds = suppliers.Select(s => s.Id).ToList();
var suppliersActivityMap = Context.Users.AsNoTracking()
.Where(e => supplierIds.Contains(e.SupplierId.Value))
.Select(e => new
{
Status = e.Status,
SupplierId = e.SupplierId.Value
})
.ToList()
.GroupBy(u => u.SupplierId)
.Select(x => new
{
SuplierId = x.Key,
AllInactive = x.All(u => u.Status != UserStatus.Active)
})
.ToDictionary(x => x.SuplierId);
foreach (var supplier in suppliers)
{
supplier.AllUsersInactive = suppliersActivityMap.ContainsKey(supplier.Id)
? suppliersActivityMap[supplier.Id].AllInactive
: true;
}
return PartialView("_Grid", suppliers);
}
UPD和我有用户控制器
public ActionResult Index(Guid id)
{
var supplierOfUser = Context.Suppliers.AsNoTracking()
//.Include(e => e.Supplier)
.FirstOrDefault(e => e.Id == id);
ViewData.Add("id", id);
ViewData.Add("SupplierFullName", supplierOfUser.FullName);
return View();
}
我需要在这里添加检查供应商的用户是否处于非活动状态,但我不知道如何进行查询。