2013-06-13 2 views
0

내 프로젝트에 공급 모델과 사용자 모델, 모든 공급자는 몇 가지 사용자를 가지고있다
공급 모델쿼리를 만들려면

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(); 
    } 

에 대한 컨트롤러를 가지고 있고 나는 그의 사용자를위한 공급 상태 비활성이있는 경우 여기에 검사를 추가해야합니다,하지만 난 쿼리를 만드는 방법을 알고하지 않습니다.

답변

0

그것은 실물없이 하드 코딩이야, 나는 이것이 올바른 희망 :

public ActionResult Grid(int? status, Pager pager, Guid? supplierId) 
{ 

    var sActive = Context.Suppliers.AsNoTracking() 
     .WhereIf(o=> o.Id == supplierId) 
     .Select(o=> o.Active).FirstOrDefault(); 

    var page = Context.Users.AsNoTracking() 
     .Where(e => e.SupplierId == supplierId) 
     .WhereIf(status != null, e => (e.Status == status)) 
     .Select(e => new UserRow 
     { 
      Id = e.Id, 
      FullName = e.FullName, 
      Email = e.Email, 
      Name = e.Name, 
      Status = e.Status, 
      Role = e.Role, 
      SupplierIsInactive = !sActive 
     }) 
     .GetPage(pager, Sorter.Asc<UserRow, string>(e => e.FullName)); 

    return PartialView("_Grid", page); 
} 
+0

내가, 내가 실수를 유감 만든거야, 지금 내가 대체 컨트롤러의 내 질문에 잘못된 부분에 추가. 해당 사용자의 공급 업체가 비활성 상태인지 확인하려면 여기에 어떻게 추가합니까? – Heidel

+0

귀하의 질문에 대답하기 위해서는 Context.Users 및 Context.Suppliers 클래스의 구조를 알아야합니다.sintax Suppliers.Users.Any (o =>! o.IsActive)를 사용해야합니다. –

관련 문제