【发布时间】:2021-11-25 07:36:08
【问题描述】:
我正在尝试在 mvc 应用程序中使用 linq 实现通配符搜索。但它不工作。请查看我的代码,我在这里缺少什么,每次我单击搜索按钮时,它都会返回相同的行数。表示搜索不工作。请帮忙
IEnumerable<ProductList> listProducts = null;
listProducts = IProductRep.GetAllProducts();
if (listProducts != null)
{
if ((!String.IsNullOrEmpty(strName)) || (!String.IsNullOrEmpty(strCategory))|| (!String.IsNullOrEmpty(strBrand)))
{
listProducts = listProducts.Where(s => s.pro_name.StartsWith(strName, StringComparison.OrdinalIgnoreCase)
|| s.CategoryName.StartsWith(strCategory,StringComparison.OrdinalIgnoreCase)
|| s.BrandName.StartsWith(strBrand, StringComparison.OrdinalIgnoreCase) );
}
ViewBag.totalRecords = listProducts.Count();
}
【问题讨论】:
-
你确定strName、strCategori和strBrand有价值吗?
-
我试图只给出一个值,这意味着如果我想按品牌进行搜索,那么我只给出谷仓的价值并将其他参数留空。
-
为什么将
null分配给listProducts,然后立即用IProductRep.GetAllProducts()覆盖它?只需分配即可。 -
未使用的过滤器变量是
null还是空的?如果null,EF 5 将忽略该部分测试,如果"",EF 5 将向SQL 发送为true 的代码。正在生成什么 SQL?
标签: entity-framework linq asp.net-mvc-5