c# - ASP.Net MVC 5 Entity Framework Select Where In? -
i have 3 tables project, province, projprovrel
in project page when add data select multiple provinces each project means province multi select dropdown list.
i insert data working inserted id , added projprovrel selected ids of province
now in details view want display data not solve it.
here code:
// get: project/details/5 public actionresult details(int? id) { if (id == null) return new httpstatuscoderesult(httpstatuscode.badrequest); var mydata = db.projects.find(id); if (mydata == null) { return httpnotfound(); } var prov_id = o in db.prorel o.projectid.equals(id) select o.provinceids; foreach (var p_id in prov_id) { int[] pids = new int[] {p_id}; } var prov= c in db.provinces c.id in pids; viewbag.province = prov; return view(mydata); }
one problem how can select data table based on condition
var prov_id = o in db.prorel o.projectid.equals(id) select o.provinceids;
is above query correct ? new asp.net mvc blow query correct ?
var prov= c in db.provinces c.id in pids;
how can select data table province province.id in pids
get rows prorel projectid matches id:
var prov_ids = db.prorel.where(r => r.projectid == id).select(r => r.provinceid);
get provinces can found prov_id:
var provs = db.provinces.where(p => prov_id.any(i => p.id == i));
i hope don't mind lambda-style linq.
also, think should consider variable naming. example, prov_id gives idea of single id , since declared type implicitly difficult tell otherwise.
Comments
Post a Comment