2014-03-07 51 views
0

我正在MVC中開發一個應用程序。 我正在使用實體框架。如何使用linq在實體框架中的項目列表中搜索項目

我想使用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;   
     } 

我剛纔提到在列表中只有3個項目,在實際應用中有很多對象在列表中。

我有下面的代碼...

 public ActionResult Index(string sectionName = "", string sectionCode = "", string size = "") 
    { 
     var inventoryProducts = from inventoryProd in AddProducts() select inventoryProd; 
     sectionName = "Section1"; 
     sectionCode = ""; 
     size = ""; 

     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())); 

     ViewBag.ProductList = inventoryProducts; 
     return View(); 
} 

現在我的問題是,當上述命令執行時,它返回所有三個記錄,我期待只有一個具有「SECTION1」內容記錄。 .. 有什麼問題?

回答

-1

更改我的代碼爲搜索 -

[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(); 
} 

,這讓我適當的值,因爲我想要的。

0

您正在搜索SectionCodeSize上的空字符串。因爲你使用的是OR語句,所以你的查詢會匹配每個對象。 String.Contains總是返回true當您搜索空字符串。請參閱http://msdn.microsoft.com/en-us/library/dy85x1sa%28v=vs.110%29.aspx

根據您的要求,您可以通過將您的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())); 
+0

我確實有同樣的問題,它有什麼解決方案,它不應該採取空白值... – bnil

+0

@ user165089檢查你的字符串是否包含空字符串沒有意義,那麼你究竟是什麼做? – Olaf

+0

我在表單上有三個文本框,我試圖通過從這些文本框中獲取輸入來搜索數據。雖然我只填寫了sectionName中的一個文本框的值 - 「Section1」,它將返回所有不包含「Section1」字符串的三條記錄... –

相關問題