EF:如何动态地将列名传递给where子句

时间:2018-02-09 12:44:34

标签: c# entity-framework

我不是在开发电脑之前。我刚开始与EF合作。很奇怪我们可以动态地为where子句传递列名。

查看用于搜索网格的屏幕截图。

enter image description here

我只是撰写一个示例查询。请告诉我它有用吗?

public ActionResult Index(String ColumnName,String SearchText)
{

    private CustomersEntities db = new CustomersEntities();

    var customer = (from s in db.Customers
                    select new CustomerDTO
                    {
                    CustomerID = s.CustomerID,
                    CompanyName = s.CompanyName,
                    ContactName = s.ContactName,
                    ContactTitle = s.ContactTitle,
                    Address = s.Address
                    })
    .Where(s => s.Field<string>(ColumnName).ToUpper().Contains(SearchText.ToUpper());

    return View(customer);

}

感谢

3 个答案:

答案 0 :(得分:2)

您可以在存储库中创建这样的内容(如果您使用它)

public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
{
    return _context.Set<CustomersEntities>().Where(predicate);
}

然后

var result = _repository.FindBy(y => y.CompanyName.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) >= 0);

答案 1 :(得分:0)

基本模式是在运行时构建查询,在运行查询之前有选择地添加Where表达式并将结果投影到DTO中。

像这样:

        public IList<CustomerDTO> FindCustomers(String ColumnName, String SearchText)
        {

            var query = from s in db.Customers select s;

            if (ColumnName == "CompanyName")
            {
                query = query.Where(c => c.CompanyName == SearchText);
            }
            else if (ColumnName == "ContactName")
            {
                query = query.Where(c => c.ContactName == SearchText);
            }
            //. . .
            else
            {
                throw new InvalidOperationException($"Column {ColumnName} not found or not supported for searching.");
            }

            var results = from c in query
                        select new CustomerDTO()
                        {
                            CustomerID = c.CustomerID,
                            CompanyName = c.CompanyName,
                            ContactName = c.ContactName,
                            ContactTitle = c.ContactTitle,
                            Address = c.Address
                        };

            return results;

        }

答案 2 :(得分:0)

public ActionResult Index(string ColumnName, string SearchText)
{
    var arg = Expression.Parameter(typeof(Customer), "x");

    var strType = typeof(string);
    var ToUpperMeth = strType.GetMethods().Where(x => x.Name == nameof(string.ToUpper) 
                                          && x.GetParameters().Count() == 0).Single();
    var ContainsMeth = strType.GetMethods().Where(x => x.Name == nameof(string.Contains) 
                                          && x.GetParameters().Count() == 1).Single();

    var exprVal = Expression.Constant(SearchText);
    var toUpExprVal = Expression.Call(exprVal, ToUpperMeth);

    var exprProp = Expression.Property(arg, ColumnName);
    var toUpExpr = Expression.Call(exprProp, ToUpperMeth);
    var contExpr = Expression.Call(toUpExpr, ContainsMeth, toUpExprVal);

    var predicate = Expression.Lambda<Func<Customer, bool>>(contExpr, arg);

    var customer = (from s in db.Customers
                    select new CustomerDTO
                    {
                        CustomerID = s.CustomerID,
                        CompanyName = s.CompanyName,
                        ContactName = s.ContactName,
                        ContactTitle = s.ContactTitle,
                        Address = s.Address
                    }).Where(predicate).ToList();

    return View(customer);
}