用于过滤页面结果的LINQ查询

时间:2016-05-06 09:13:29

标签: c# asp.net-mvc entity-framework linq

我拥有的表/模型:UsersHospitalsUserHospitalsUserAcessesUserAcessesViewModel

我有一个显示所有用户acesses的页面。

我通过链接/ UserAcess / 1发送医院ID

我需要查询用户是否与表UserHospitals上的医院ID有关联,如果是,则显示连接到同一医院ID的用户的所有acesses。

我所拥有的和我的查询不正确:

    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

如果您需要更多信息,我的医院 - 用户医院 - 用户,它是here

1 个答案:

答案 0 :(得分:0)

如果需要,请在选择列时进行一些改进。

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