C# 使用实体框架按整数筛选结果
我有一个MVC项目,我正在使用实体框架 我在数据库中有一些数据需要使用复选框按整数变量(时间)进行过滤。我收到一条错误消息: 参数字典包含“LazyRecipe.DAL.RecipesController”中方法“System.Web.Mvc.ViewResult Index(System.String,System.String,System.String[],Int32)”的非null类型“System.Int32”的参数“time”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数 如何过滤数据C# 使用实体框架按整数筛选结果,c#,asp.net-mvc,entity-framework,checkbox,filtering,C#,Asp.net Mvc,Entity Framework,Checkbox,Filtering,我有一个MVC项目,我正在使用实体框架 我在数据库中有一些数据需要使用复选框按整数变量(时间)进行过滤。我收到一条错误消息: 参数字典包含“LazyRecipe.DAL.RecipesController”中方法“System.Web.Mvc.ViewResult Index(System.String,System.String,System.String[],Int32)”的非null类型“System.Int32”的参数“time”的null条目。可选参数必须是引用类型、可为null的类型
// 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.IngredientName)));
}
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);
}
//获取:配方
公共视图结果索引(字符串排序器、字符串搜索字符串、字符串[]FilteredsearchString、整数时间)
{
可饮用配方;
if(String.IsNullOrEmpty(searchString))
{
配方=db.配方。包括(“成分”);
}
其他的
{
FilteredsearchString=searchString.Split(默认值(字符串[]),StringSplitOptions.RemoveEmptyEntries);
//“string”可以是小写。
WriteLine(string.Join(“,”,FilteredsearchString));
//…“String”可以是大写。
WriteLine(String.Join(“,”,FilteredsearchString));
recipes=db.recipes.Where(r=>r.components.Any(i=>FilteredsearchString.Contains)(i.IngredientName),;
}
开关(时间)
{
案例30:
配方=db.recipes.Where(c=>c.Time.CompareTo(Time)c.Time.CompareTo(Time)c.Time.CompareTo(Time)>=61);
打破
违约:
配方=db.配方;
打破
}
返回视图(配方);
}
至于switch
语句,因为在这些常量中,您一直将时间与time
的值进行比较,所以不需要写三次。您只需检查time
是30
、60
还是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);
}
公共视图结果索引(字符串排序器、字符串搜索字符串、字符串[]FilteredsearchString、整数时间)
{
IQueryable配方=db.配方。包括(“成分”);
如果(!string.IsNullOrEmpty(searchString))
{
recipes=recipes.Where(r=>r.components.Contains(searchString));
}
如果(时间=30 | |时间=60 | |时间=61)
{
recipes=receips.Where(c=>c.Time)消息是自解释的-您没有传递
Time
参数的值。Where(c=>c.Time.CompareTo(Time)时间是的,这就是我真正想要的!我应该在开关中传递什么?这可能会更正OP的一些代码,但它与抛出的问题和异常无关
recipes = recipes.Where(c => c.Time <= 30);
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);
}