MVC5 如何在 Controller 中过滤 'equal to or less than'
MVC5 How to filter for 'equal to or less than' in Controller
我对 MVC5 和 C# 都很陌生。我有一个耗材视图(列表)。我想对 int 字段 'Onhand' 进行过滤。其中,用户可以在筛选文本框中输入一个数字,我们将手头等于或小于指定数量的所有物资带回来。
以下是我观点的相关部分:
<form asp-controller="Movies" asp-action="Index">
<p>
Search Supplies: <input type="text" name="SearchString">
<input type="submit" value="Filter" />
</p>
<p>
</p>
</form>
这就是我努力让控制器正常工作的地方
var supplies = db.ICS_Supplies
.OrderByDescending(g => g.Supplies_ID)
.ToList();
var catalogs = supplies
.Where(s => s.OnHand.HasValue<(searchString ?? string.Empty));
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
特别是。 .这行在这里:
var catalogs = supplies
.Where(s => s.OnHand.HasValue<(searchString ?? string.Empty));
我觉得我快到了,但不太了解如何通过 <=
int
searchstring
的值到达那里。 OnHand
是一个 int?
字段。我是否也需要将 searchString
设为 int
?我的过滤器正确吗?
更新
这是修改后的控制器代码。 .截至目前
public ActionResult Reorder(string searchString, int? page)
{
var supplies = db.ICS_Supplies
.OrderByDescending(g => g.Supplies_ID)
.ToList();
var catalogs = supplies
.Where(s =>
s.OnHand.HasValue &&
(searchString == null || s.OnHand.Value <= int.Parse(searchString)));
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}
将我们的对话浓缩为一个答案,您说您对这行代码有疑问:
var catalogs = supplies.Where(s => s.OnHand.HasValue < (searchString ?? string.Empty));
这里有几个问题。首先,HasValue
return 是一个 bool
,所以我们不能与它进行 <
比较。相反,如果 HasValue
是 true
,那么我们要对 Value
属性:
进行比较
var catalogs = supplies.Where(s => s.OnHand.HasValue && s.OnHand.Value < searchValue)
但是,我们需要使用 int
代替上面的 searchValue
,并且在您的原始代码中您使用的是字符串。所以我们需要将字符串转换为 int
,最安全的方法是 int.TryParse
,其中 return 是一个 bool
表示成功,并设置 out
参数为转换后的值如果成功。
既然你说如果字符串是 null
我们应该 return 所有值,那么我们应该从字符串创建一个值,如果它是 null
它将是最大值。所以我们可以这样做:
int searchValue;
// If searchString is null (or not an int), then set our searchValue to int.MaxValue
if (!int.TryParse(searchString, out searchValue)) searchValue = int.MaxValue;
现在我们可以使用这个 searchValue
来过滤我们的查询:
var catalogs = supplies.Where(s => s.OnHand.HasValue && s.OnHand.Value <= searchValue)
这不是答案...但旨在帮助您,这很难读...
public ActionResult Reorder(string searchString, int? page)
{
/// this has performance issues
var supplies = db.ICS_Supplies.OrderByDescending(g => g.Supplies_ID).ToList();
// problem here is you are checking for null, in a c# way and not linq
var catalogs = supplies.Where(s => s.OnHand.HasValue && (searchString == null || s.Value <= int.Parse(searchString)));
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}
这需要更多的工作,但更容易阅读...希望它对您有所帮助
public ActionResult Reorder(string searchString, int? page)
{
/check your string first
if(searchString == null)
searchString = "";
///from the below im guessing searchString is an int
var int value = int.Parse(searchString);
var supplies = db.ICS_Supplies.Where(x => x.OnHand != null
&& searchString.Contians(x.FieldName)
.OrderByDescending(x => x.Supplies_ID)
.ToList(); // to list forces a pull of the results
//other stuff to work on
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}
public ActionResult Reorder(string searchString, int? page)
{
int convertInt = 0;
var catalogs = db.ICS_Supplies
.OrderByDescending(g => g.Supplies_ID)
.ToList();
if (Int32.TryParse(searchString, out convertInt))
{
catalogs = supplies
.Where(s =>
s.OnHand.HasValue &&
s.OnHand.Value <= convertInt);
}
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}
我对 MVC5 和 C# 都很陌生。我有一个耗材视图(列表)。我想对 int 字段 'Onhand' 进行过滤。其中,用户可以在筛选文本框中输入一个数字,我们将手头等于或小于指定数量的所有物资带回来。
以下是我观点的相关部分:
<form asp-controller="Movies" asp-action="Index">
<p>
Search Supplies: <input type="text" name="SearchString">
<input type="submit" value="Filter" />
</p>
<p>
</p>
</form>
这就是我努力让控制器正常工作的地方
var supplies = db.ICS_Supplies
.OrderByDescending(g => g.Supplies_ID)
.ToList();
var catalogs = supplies
.Where(s => s.OnHand.HasValue<(searchString ?? string.Empty));
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
特别是。 .这行在这里:
var catalogs = supplies
.Where(s => s.OnHand.HasValue<(searchString ?? string.Empty));
我觉得我快到了,但不太了解如何通过 <=
int
searchstring
的值到达那里。 OnHand
是一个 int?
字段。我是否也需要将 searchString
设为 int
?我的过滤器正确吗?
更新
这是修改后的控制器代码。 .截至目前
public ActionResult Reorder(string searchString, int? page)
{
var supplies = db.ICS_Supplies
.OrderByDescending(g => g.Supplies_ID)
.ToList();
var catalogs = supplies
.Where(s =>
s.OnHand.HasValue &&
(searchString == null || s.OnHand.Value <= int.Parse(searchString)));
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}
将我们的对话浓缩为一个答案,您说您对这行代码有疑问:
var catalogs = supplies.Where(s => s.OnHand.HasValue < (searchString ?? string.Empty));
这里有几个问题。首先,HasValue
return 是一个 bool
,所以我们不能与它进行 <
比较。相反,如果 HasValue
是 true
,那么我们要对 Value
属性:
var catalogs = supplies.Where(s => s.OnHand.HasValue && s.OnHand.Value < searchValue)
但是,我们需要使用 int
代替上面的 searchValue
,并且在您的原始代码中您使用的是字符串。所以我们需要将字符串转换为 int
,最安全的方法是 int.TryParse
,其中 return 是一个 bool
表示成功,并设置 out
参数为转换后的值如果成功。
既然你说如果字符串是 null
我们应该 return 所有值,那么我们应该从字符串创建一个值,如果它是 null
它将是最大值。所以我们可以这样做:
int searchValue;
// If searchString is null (or not an int), then set our searchValue to int.MaxValue
if (!int.TryParse(searchString, out searchValue)) searchValue = int.MaxValue;
现在我们可以使用这个 searchValue
来过滤我们的查询:
var catalogs = supplies.Where(s => s.OnHand.HasValue && s.OnHand.Value <= searchValue)
这不是答案...但旨在帮助您,这很难读...
public ActionResult Reorder(string searchString, int? page)
{
/// this has performance issues
var supplies = db.ICS_Supplies.OrderByDescending(g => g.Supplies_ID).ToList();
// problem here is you are checking for null, in a c# way and not linq
var catalogs = supplies.Where(s => s.OnHand.HasValue && (searchString == null || s.Value <= int.Parse(searchString)));
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}
这需要更多的工作,但更容易阅读...希望它对您有所帮助
public ActionResult Reorder(string searchString, int? page)
{
/check your string first
if(searchString == null)
searchString = "";
///from the below im guessing searchString is an int
var int value = int.Parse(searchString);
var supplies = db.ICS_Supplies.Where(x => x.OnHand != null
&& searchString.Contians(x.FieldName)
.OrderByDescending(x => x.Supplies_ID)
.ToList(); // to list forces a pull of the results
//other stuff to work on
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}
public ActionResult Reorder(string searchString, int? page)
{
int convertInt = 0;
var catalogs = db.ICS_Supplies
.OrderByDescending(g => g.Supplies_ID)
.ToList();
if (Int32.TryParse(searchString, out convertInt))
{
catalogs = supplies
.Where(s =>
s.OnHand.HasValue &&
s.OnHand.Value <= convertInt);
}
var pageNumber = page ?? 1;
return View(catalogs.ToPagedList(pageNumber, 10));
}