使用linq子查询
子查询
法一:使用linq语句
//获得核算科室编号
IEnumerable<string> s = (from a in db.Fee_Depts
where a.院区 == "**院区"
select a.核算科室编号).Distinct();
//获得包括核算科室编号名称
var depts = from a in db.Fee_Depts
where a.院区 == "**院区" && (s.Contains(a.FeeDeptID))
select new SelectListItem
{
Text = a.FeeDeptName,
Value = a.FeeDeptID
};
ViewData["DeptList"] = depts.Distinct();
在razor页面中调用
<span class="text"> 科室</span>  
@Html.DropDownList("deptID", @ViewData["DeptList"] as IEnumerable<SelectListItem>, new { @readonly = "readonly", style = "width:300px" })
法二:使用原生sql语句
var r = db.Fee_Depts.SqlQuery("select * from Fee_Depts where FeeDeptID in (select distinct 核算科室编号 from Fee_Depts ) and 院区 ='**院区'");