【问题标题】:How to search a database using properties of model如何使用模型的属性搜索数据库
【发布时间】:2014-02-13 00:03:41
【问题描述】:

我有一个 MVC4 程序,其中有一个下拉列表,其中列出了模型的所有属性。我希望用户能够选择哪个属性,然后在文本框中键入要搜索的字符串值。问题是我无法使用下拉列表的值动态更改查询。

public ActionResult SearchIndex(string searchString, string searchFields)
    {
        var selectListItems = new List<SelectListItem>();
        var first = db.BloodStored.First();
        foreach(var item in first.GetType().GetProperties())
        {
            selectListItems.Add(new SelectListItem(){ Text = item.Name, Value = item.Name});
        }
        IEnumerable<SelectListItem> enumSelectList = selectListItems;
        ViewBag.SearchFields = enumSelectList;

        var bloodSearch = from m in db.BloodStored
                          select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            PropertyInfo selectedSearchField = getType(searchFields);
            string fieldName = selectedSearchField.Name;

            //Dynamic Linq query this is how it needs to be set up to pass through linq.dynamic without exception
            bloodSearch = bloodSearch.Where("x => x." + fieldName + " == " + "@0", searchString).OrderBy("x => x." + fieldName);
            return View(bloodSearch); 
        }
        return View(bloodSearch);
    }

这是我的 getType 方法

public PropertyInfo getType(string searchFields)
    {
        var first = db.BloodStored.First();
        foreach (var item in first.GetType().GetProperties())
        {
            if(searchFields == item.Name)
            {
                return item;
            }
        }
        return null;
    }

我已经更新了我的代码以反映工作查询,它​​可以帮助其他任何人。 这是我从Dynamic query with LINQ won't work找到答案的帖子的链接

【问题讨论】:

    标签: c# asp.net-mvc-4 lambda


    【解决方案1】:

    您应该使用 Dynamic.cs 库。您可以在此处找到它以及示例。然后,您可以动态创建 where 子句。

    dynamic.cs

    【讨论】:

    • +1 我不知道 Linq 动态查询库。
    • 我一辈子都无法得到查询来做我想做的事。我不确定它到底是如何工作的,而且它的有限示例不知道太多。如果您能提供帮助,我将在上面附上我的查询,我们将不胜感激。
    • where 函数就像条件语句一样,应该使用“==”哦,并且需要使用正确的 lambda 语法。
    • bloodSearch = bloodSearch.Where("x => x." + fieldName + " == " + searchString).OrderBy("x => x." + fieldName);
    • 我遇到了和以前一样的问题,但它似乎只搜索列名而不是实际数据。我收到 ParseError(“BloodStoredModel”类型中不存在属性或字段“Smith”)Smith 只是数据库中的名称之一。
    【解决方案2】:

    您可以自己构建表达式。

    static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue)
    {
        var parameterExp = Expression.Parameter(typeof(T), "type");
        var propertyExp = Expression.Property(parameterExp, propertyName);
        MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
        var someValue = Expression.Constant(propertyValue, typeof(string));
        var containsMethodExp = Expression.Call(propertyExp, method, someValue);
    
        return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
    }
    

    这是从 Marc Gravell 的帖子中偷来的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 2014-06-26
      • 2021-04-03
      • 1970-01-01
      • 2010-11-18
      • 2019-04-14
      • 2015-02-05
      相关资源
      最近更新 更多