c# - MVC View 中显示的 Linq 连接查询

标签 c# asp.net-mvc linq join

我正在尝试在部分 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/

相关文章:

c# - Log4Net:来自引用 DLL 的自定义附加程序被忽略

html - 是否可以访问谷歌浏览器的主题?

jquery - jquery如何获取所有文件类型的输入控件

c# - XML LINQ 查询不返回任何内容

c# - 同时检查多个对象的 bool 属性

c# - 使用 XAdES 数字签名的库

c# - 如何创建包含一个字符串变量和三个 int 变量的字典

asp.net-mvc - FineUploader 无法 POST

c# - 间歇性 Linq FirstOrDefault 错误 - 索引超出数组范围

sql - 从 LINQ 获取 SQL 字符串到 EF 查询