我正在尝试在部分 Web View 中显示 linq 连接查询。
这是我在 Controller 中的查询:
public ActionResult InactiveUsers()
{
using (ModelContainer ctn = new ModelContainer())
{
DateTime duration = DateTime.Now.AddDays(-3);
var inactive = from usrs in ctn.aspnet_Users
where usrs.LastActivityDate <= duration
join o in ctn.Groups on
usrs.UserId equals o.UserID
select new
{
usrs.UserName,
usrs.LastActivityDate,
o.PrimaryPhoneNumber,
};
return View(inactive.ToList());
}
}
我有点困惑的是下一步该做什么。我熟悉使用模型添加强类型 View ,但在我有连接查询的情况下会发生什么情况?
如果有人能指出正确的方向,我将不胜感激。
谢谢。
最佳答案
一种解决方案是使用“ViewModel”模式。不要创建匿名类型,而是创建一个包含要显示的数据的 View 模型。只需填充它并将其传递给您的 View 即可。
When using this pattern we create strongly-typed classes that are optimized for our specific view scenarios, and which expose properties for the dynamic values/content needed by our view templates. Our controller classes can then populate and pass these view-optimized classes to our view template to use. This enables type-safety, compile-time checking, and editor intellisense within view templates.
关于c# - MVC View 中显示的 Linq 连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4318739/