【问题标题】:Filter results by integer with Entity Framework使用实体框架按整数过滤结果
【发布时间】:2018-05-28 10:50:39
【问题描述】:

我有一个 MVC 项目,我正在使用实体框架。

我在数据库中有一些数据需要使用复选框通过整数变量(时间)进行过滤。我收到一条错误消息:

参数字典包含方法'System.Web.Mvc.ViewResult Index(System.String, System.String, System.String[]的不可空类型'System.Int32'的参数'time'的空条目, Int32)' 在'LazyRecipe.DAL.RecipesController' 中。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数

如何过滤数据?

// GET: Recipes
    public ViewResult Index(string sortOrder, string searchString, string[] FilteredsearchString, int time)
    {   
        IQueryable recipes;
        if (String.IsNullOrEmpty(searchString))
        {
            recipes = db.Recipes.Include("Ingredients");
        }
        else
        {
            FilteredsearchString = searchString.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);

            // "string" can be lowercase.
            Console.WriteLine(string.Join(",", FilteredsearchString));

            // ... "String" can be uppercase.
            Console.WriteLine(String.Join(",", FilteredsearchString));

            recipes = db.Recipes.Where(r => r.Ingredients.Any(i => FilteredsearchString.Contains(i.Ingredi‌​entName)));
        }

        switch(time)
        {


            case 30:
            recipes = db.Recipes.Where(c => c.Time.CompareTo(time) <= 30);
            break;

            case 60:
            recipes = db.Recipes.Where(c => c.Time.CompareTo(time) <= 60);
            break;

            case 61:
            recipes = db.Recipes.Where(c => c.Time.CompareTo(time) >= 61);
            break;

            default:
                recipes = db.Recipes;
                break;
        }


        return View(recipes);
    }

【问题讨论】:

  • 该消息是不言自明的 - 您没有为 time 参数传递值。
  • Where(c =&gt; c.Time.CompareTo(time) &lt;= 30) - 我想你的意思是Where(c =&gt; c.Time &lt;= 30);
  • 是的,这就是我真正想要的!我应该在 switch 中传递什么?

标签: c# asp.net-mvc entity-framework checkbox filtering


【解决方案1】:
case 30:
    recipes = db.Recipes.Where(c => c.Time.CompareTo(time) <= 30);
    break;

我认为您的意思是简单地将c.Time 与常量30 进行比较。因此,您只需执行.Where(c =&gt; c.Time &lt;= 30).Where(c =&gt; c.Time &lt;= time),因为time 与您要比较的值具有相同的值。

请注意,您不应始终从db.Recipes 开始查询。通过执行recipes = db.Recipes.Where(…),您将覆盖您之前已经为recipes 设置的任何查询。例如,代码中第一个 if 中的 .Include("Ingredients") 将被覆盖。

相反,只需继续调用recipes 上的查询方法以继续完善它:

recipes = recipes.Where(c => c.Time <= 30);

至于您的switch 语句,由于您在每种情况下都将时间与time 的值进行比较,因此您不需要写三遍。您只需检查time3060 还是61,然后与time 进行比较。

总的来说,您的方法可能如下所示:

public ViewResult Index(string sortOrder, string searchString, string[] FilteredsearchString, int time)
{
    IQueryable recipes = db.Recipes.Include("Ingredients");

    if (!string.IsNullOrEmpty(searchString))
    {
        recipes = recipes.Where(r => r.Ingredients.Contains(searchString));
    }

    if (time == 30 || time == 60 || time == 61)
    {
        recipes = recieps.Where(c => c.Time <= time);
    }

    return View(recipes);
}

请注意,您应该修改您的 searchString 逻辑。你传递searchStringFilteredsearchString 数组对我来说真的没有意义,然后通过拆分searchString 来填充?您可能是说 FilteredsearchString 是这里的局部变量?

【讨论】:

  • 这可能会纠正一些 OP 的代码,但它与问题和抛出的异常完全无关
猜你喜欢
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 2014-12-19
  • 1970-01-01
  • 2011-06-18
  • 2019-04-14
  • 2017-11-08
  • 1970-01-01
相关资源
最近更新 更多