2016-05-06 158 views
0

我有:Users,Hospitals,UserHospitals,UserAcessesUserAcessesViewModel表/模型。LINQ查詢過濾頁面結果

我有一個顯示所有用戶訪問的頁面。

我通過鏈接發送給醫院ID/UserAcess/1

我需要查詢,如果用戶對錶UserHospitals到醫院ID的連接,如果這是真的,顯示來自用戶所有acesses被連接到相同的醫院ID。

這就是我和我的查詢是不正確的:

public ActionResult Index(int? id, string searchString, string sortOrder, string SelectedUserId) 
    { 
     if(!User.IsInRole("Admin")) 
     { 
      if (id == null) 
      { 
       return View("Error"); 

      } 
      else if (!db.Hospitals.Any(o => o.HospitalID == id)) 
      { 
       return View("Error"); 
      } 

      var model = new UserAccessViewModel(); 
      var userAccesses = db.UserAccesses.Include(u => u.User); 

      ViewBag.IDSortParm = sortOrder == "ID_asc" ? "ID_desc" : "ID_asc"; 
      ViewBag.EmailSortParm = sortOrder == "Email_asc" ? "Email_desc" : "Email_asc"; 
      ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; 
      ViewBag.NameSortParm = sortOrder == "Name_asc" ? "Name_desc" : "Name_asc"; 

      var userID = User.Identity.GetUserId(); 
      var result = db.Hospitals.Include("UserHospitals") 
           .Where(x => x.UserHospitals 
           .Any(u => u.Id == userID && u.HospitalID == id)) 
           .FirstOrDefault(); 

      if (result != null) 
      { 
       userAccesses = db.UserAccesses.Include(u => u.User).Include("UserHospitals").Where(w => w.User.any()))); 
      } 
      else 
      { 
       return View("Error"); 
      } 
     } 

我的模型:

醫院

public class Hospital 
{ 
    [Key] 
    public int HospitalID { get; set; } 

    public string Name { get; set; } 

    public virtual ICollection<HospitalSpeciality> HospitalSpecialities { get; set; } 
    public virtual ICollection<UserHospital> UserHospitals { get; set; } 
} 

UserHospital

public class UserHospital 
{ 
    [Key] 
    public int UserHospitalID { get; set; } 

    public int HospitalID { get; set; } 
    public Hospital Hospitals { get; set; } 

    public string Id { get; set; } 
    public ApplicationUser Users { get; set; } 
} 

UserAcesses

public class UserAccess 
{ 
    [Key] 
    public int UserAcessID { get; set; } 
    public DateTime Access { get; set; } 



    public string UserID { get; set; } 
    [ForeignKey("UserID")] 

    public ApplicationUser User { get; set; } 
} 

UserAcessViewModel

public class UserAccessViewModel 
{ 
    [Key] 
    public int ID { get; set; } 
    public IEnumerable<UserAccess> UserAccesses { get; set; } 

    [Display(Name = "Selecionar Utilizador")] 
    public string SelectedUserId { get; set; } 

    public IEnumerable<SelectListItem> UserNamesList; 
} 

有我table's connection

如果您需要abount我院更多信息 - UserHospital - 用戶,這是here

回答

0

如果需要在選擇請做一些細化列。

List<UserHospitals> _userHospitals= (from p in obj.UserHospitals 
where (p.HospitalID == _hospitalId || p.IsStatus==1) 
.GroupBy(p => p.HospitalID) 
select new {}.ToList(); 
+0

感謝您的回答,但我不明白它 –

+0

你想要連接到醫院的所有訪問id.Am我是吧? – stylishCoder

+0

我希望來自用戶的所有訪問都具有連接到UserHospitals上當前醫院ID的ID。 [這是你的代碼的結果](https://i.gyazo.com/663b08c36fa1dd3c740577153b07a6be.png) –