c# - MVC Linq 查询在 WHERE 子句中使用动态列名

标签 c# .net asp.net-mvc linq

我对同一查询进行了多次调用,但 WHERE 子句略有不同,有谁知道是否可以传递变量作为列名,因为我似乎无法实现它.

我知道以下内容不正确,但只是为了让您了解我想要实现的目标。

public EmailUserListViewModel EmailUserListData(int CaseId, string ColumnName)
{
    CaseId = CaseId,
    EmailUserList = (from u in efContext.PgsUsers
                        where ColumnName == true
                        orderby u.FirstName, u.LastName
                        select new EmailUserListModel
                        {
                            UserId = u.Id,
                            Name = ((u.FirstName == null) ? "" : u.FirstName) 
                                   + ((u.LastName == null) ? "" : " " + u.LastName),
                            Email = u.Email,
                            Checked = false

                        }).ToList()
    };
}

最佳答案

我想你可以使用反射来动态检索属性的值

from u in efContext.PgsUsers where (typeof(PgsUser).GetProperty(ColumnName).GetValue(u) as bool) == true

from u in efContext.PgsUsers where (u.GetType().GetProperty(ColumnName).GetValue(u) as bool) == true

关于c# - MVC Linq 查询在 WHERE 子句中使用动态列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41742052/

相关文章:

c# - 构建 LINQ 查询以从字符串字典/字符串字典/字符串字典中获取字典列表?

c# - 将 Windows 身份验证与声明混合使用

c# - 如何避免 MEF 中的冗余导入?

.net - 有没有办法以编程方式确定 .NET 进程中加载​​了哪些 native DLL?

asp.net-mvc - 什么是 ASP 的 ContentPlaceHolder 的 Grails GSP 等价物?

c# - 如何从 AuthorizeAttribute 自定义类的 HttpActionContext 获取 cookie?

javascript - 如何在wcf服务中获取Xml作为字符串并使用javascript发送?

c# - 发布选项缺少 Visual Studio 2015 Professional

c# - 要在 sitecore 的新选项卡中打开的返​​回 URL 的保留类型应该是什么?

c# - 结构图配置异常