Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何使用linq从实体框架中的项目列表中搜索项目_Asp.net Mvc_Entity Framework_Entity Framework 5 - Fatal编程技术网

Asp.net mvc 如何使用linq从实体框架中的项目列表中搜索项目

Asp.net mvc 如何使用linq从实体框架中的项目列表中搜索项目,asp.net-mvc,entity-framework,entity-framework-5,Asp.net Mvc,Entity Framework,Entity Framework 5,我正在用MVC开发一个应用程序。 我使用的是实体框架 我想使用LINQ命令从对象列表中搜索对象 我有以下对象的结构 public class Inventory { public int Id { get; set; } public string SectionName { get; set; } public string SectionCode { get; set; } pu

我正在用MVC开发一个应用程序。 我使用的是实体框架

我想使用LINQ命令从对象列表中搜索对象

我有以下对象的结构

 public class Inventory
        {
            public int Id { get; set; }
            public string SectionName { get; set; }
            public string SectionCode { get; set; }
            public double Size { get; set; }
         }
下面的方法将对象添加到列表中

  private List<Inventory> AddProducts()
        {
            List<Inventory> ProductList = new List<Inventory>();

            Inventory oInventory = new Inventory();
            oInventory.Id = 1;
            oInventory.SectionName = "Section1";
            oInventory.Size = 23;
            oInventory.SectionCode = "148587";
             ProductList.Add(oInventory);


            oInventory = new Inventory();
            oInventory.Id = 2;
            oInventory.SectionName = "Section2";
            oInventory.Size = 23;
            oInventory.SectionCode = "142694";
            ProductList.Add(oInventory);

            oInventory = new Inventory();
            oInventory.Id = 3;
            oInventory.SectionName = "Section3";
            oInventory.Size = 23;
            oInventory.SectionCode = "202587";
            ProductList.Add(oInventory);   


            return ProductList;           
        }
现在我的问题是,当上面的命令执行时,它返回所有三条记录,我只希望有一条记录包含“Section1”内容。。。
问题是什么

您正在
SectionCode
Size
上搜索空字符串。由于您使用的是OR语句,因此您的查询将匹配每个对象<代码>字符串。当您搜索空字符串时,Contains将始终返回
true
。看

根据您的要求,您可以通过将ORS更改为ANDS来确保所有字段与过滤器匹配。这将导致每个对象都需要使每个字段与过滤器匹配

inventoryProducts = inventoryProducts.Where(emp => emp.SectionName.ToUpper().Contains(sectionName.ToUpper().Trim())
&& emp.SectionCode.ToUpper().Contains(sectionCode.ToUpper().Trim())
&& emp.Size.ToString().ToUpper().Contains(size.ToUpper().Trim()));
或者,如果您只想筛选已填充的字段,在这种情况下,您应该只筛选使用String.IsNullOrWhiteSpace填充的字段。这将导致每个对象只需要一个字段来匹配过滤器

if (!String.IsNullOrWhiteSpace(sectionName.ToUpper().Trim()))
    inventoryProducts = inventoryProducts.Where(emp => emp.SectionName.ToUpper().Contains(sectionName.ToUpper().Trim());
if (!String.IsNullOrWhiteSpace(sectionCode.ToUpper().Trim()))
    inventoryProducts = inventoryProducts.Where(emp => emp.SectionCode.ToUpper().Contains(sectionCode.ToUpper().Trim());
if (!String.IsNullOrWhiteSpace(size.ToUpper().Trim()))
    inventoryProducts = inventoryProducts.Where(emp => emp.Size.ToString().ToUpper().Contains(size.ToUpper().Trim()));

我更改了搜索的代码作为-

[HttpPost]
public ActionResult Index(FormCollection oFormCollection, int? page)
{
  var pageNumber = page ?? 1;
  var pagesize = 5;
  var inventoryProducts = from inventoryProd in AddProducts() select inventoryProd;

  string sectionName = oFormCollection["sectionName"];
  string sectionCode = oFormCollection["sectionCode"];
  string size = oFormCollection["size"].ToString();

  var searchedResult = (from e in inventoryProducts
                       where e.SectionName == sectionName.ToUpper().Trim()
                          || e.SectionCode == sectionCode.ToUpper().Trim()                                     
                          || e.Size.ToString() == size.Trim()               
                          select e).ToList();
  ViewBag.SectionName = sectionName;
  ViewBag.SectionCode = sectionCode;
  ViewBag.Size = size;
  ViewBag.ProductList = searchedResult.ToPagedList(pageNumber, pagesize);
  return View();
}

它会给我想要的适当值。

我也有同样的问题,解决方法是什么,它不应该接受空值…@user165089检查字符串是否包含空字符串没有意义,那个么你们到底想做什么呢?我的表单上有三个文本框,我试图通过从这些文本框中输入来搜索数据。虽然我在sectionName-“Section1”中只有一个文本框中有填充值,但它返回所有不包含“Section1”字符串的三条记录……我添加了一些代码。您可以检查所有字段是否与查询匹配,或者只对不为空的筛选器进行筛选。但如果我选择按sectionName和size搜索,则此时此代码只返回按sectionName搜索的内容,而不返回大小。如果我要检查搜索sectionName大小或sectionCode大小或sectionName sectionCode的组合,该怎么办?
[HttpPost]
public ActionResult Index(FormCollection oFormCollection, int? page)
{
  var pageNumber = page ?? 1;
  var pagesize = 5;
  var inventoryProducts = from inventoryProd in AddProducts() select inventoryProd;

  string sectionName = oFormCollection["sectionName"];
  string sectionCode = oFormCollection["sectionCode"];
  string size = oFormCollection["size"].ToString();

  var searchedResult = (from e in inventoryProducts
                       where e.SectionName == sectionName.ToUpper().Trim()
                          || e.SectionCode == sectionCode.ToUpper().Trim()                                     
                          || e.Size.ToString() == size.Trim()               
                          select e).ToList();
  ViewBag.SectionName = sectionName;
  ViewBag.SectionCode = sectionCode;
  ViewBag.Size = size;
  ViewBag.ProductList = searchedResult.ToPagedList(pageNumber, pagesize);
  return View();
}