按多个专业和地点选择结果

时间:2014-01-04 03:58:48

标签: asp.net asp.net-mvc-4 c#-4.0 entity-framework-5

我是MVC&的新手EF。我正在通过专业和地点为搜索医生开发一个网站。  医生是多个地点(实践)和多个专业。这是我的模特:

public class Doctors
{
    public int DoctorsID { get; set; }
    public string DoctorName { get; set; }
    public int Fee { get; set; }
    public virtual ICollection<Practice> Practice { get; set; }
    public virtual ICollection<DoctorSpecialtys> DoctorSpecialtys { get; set; }
}

public class Practice
{
    public int PracticeID { get; set; }
    public string clinicname { get; set; }
    public string locality { get; set; }
    public int DoctorsID { get; set; }
    public virtual Doctors Doctors { get; set; }
}

public class DoctorSpecialtys
{
    public int DoctorSpecialtysID { get; set; }
    public string specialty { get; set; }
    public int DoctorsID { get; set; }
    public virtual Doctors Doctors { get; set; }
}

我正在尝试根据专业和位置获得结果。这是我的控制动作结果:

public ActionResult Index(string specialty, string location,string currentFilter,int? page)
{
    ViewBag.CurrentSort = specialty;
    ViewBag.loca = location;

    DoctorsContext db = new DoctorsContext();
    var docs = from cust in db.doctorsinfo.Where(c => c.DoctorspecialityName.Contains(specialty)).OrderBy(s=>s.Fee)
               select cust;

    //docs = docs.Distinct();
    int pageSize = 10;
    int pageNumber = (page ?? 1);
    return View(docs.ToPagedList(pageNumber, pageSize));
    //return View(docs);

}

2 个答案:

答案 0 :(得分:1)

这应该这样做。

var docs = from cust in db.doctorsinfo.
           where cust.Practice.Any(p => p.locality.Contains(location) || 
                 cust.DoctorSpecialtys.Any(ds => ds.specialty.Contains(specialty))
           order by cust.Fee
           select cust;

作为建议,您应该注意命名您的类和属性。类不应该是多元化的,集合应该是。 (您的班级名称应为DoctorPracticeDoctorSpecialty,或者只是SpecialtyDoctor上的收藏集应为Practices和{{ 1}})加上应该保持一个坚定的房地产资本化模式。 Specialtieslocation均为小写,其他类似于clinicname的是UpperCamel。您使用哪种命名约定并不重要lowerCamel,UpperCamel,alllower,但选择一个并坚持下去:D您的代码将更具可读性。

我个人最喜欢的一种个人战争,正如 {@ 3}} 使用有意义的名字DoctorSpecialtys的{​​{1}}部分没有说什么。 每个属性/字段或每个类都是一条信息。因此,添加尾随info只是噪音。那么为什么不删除它并只调用属性DoctorsContext.doctorsinfo

答案 1 :(得分:0)

DoctorsContext db = new DoctorsContext();
        var docs = from cust in db.doctorsinfo.Where(c => c.DoctorspecialityName.Contains(specialty) && c.DoctorLocation.Contains(location)).OrderBy(s=>s.Fee)
                   select cust;