[英]Linq to join two tables using a value to where clause dynamically
I want an output where the U_CCode
of first table, which is also a foreign key to my second table can be used to join both to show something like this (note : here I have passed A1
as parameter to where clause but I want it to dynamically select whatever value of U_CCode
I assign to the controller by a string variable. I want to achieve this using LINQ
and I am not able to understand how to use it in MVC controller. 我想要一个输出,其中第一张表的U_CCode
也是我第二张表的外键,可用于将两者U_CCode
以显示类似的内容(注意:这里我已将A1
作为参数传递给where子句,但我希望它动态地选择我通过字符串变量分配给控制器的U_CCode
任何值,我想使用LINQ
实现此目的,但我不明白如何在MVC控制器中使用它。
I want to assign the output of this query to a selectlist
which I want to use in my view via viewbag
. 我想将分配输出这个查询的一个selectlist
,我想在通过我的视图中使用viewbag
。
I am new to this, can someone help me with the script for this query? 我是新手,有人可以帮我提供此查询的脚本吗?
CODE 码
public ActionResult Create(string U_CCode)
{
var departments = db.Class.Where(q => q.U_CCode == U_CCode);
ViewBag.SelectedDepartment = new SelectList(departments, "U_CLCode", "U_CLName");
}
Try like this: (EDITED) 尝试这样:( 编辑)
var departmentsQuery = (from e in db.Class where e.U_CCode==U_CCode select e).ToList();
ViewBag.SelectedDepartment = departmentsQuery;
Pass it to the view.. 将其传递给视图。
In view: 鉴于:
@Html.DropDownList(
"name",
new SelectList(
((List<yourTypename>)ViewBag.SelectedDepartment).Select(x => new { Value = x.U_CCode, Text = x.U_CLName }),
"Value",
"Text"
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.