Domanda

I am developing an application in MVC. I am using entity framework.

I want to search an object from list of objects using LINQ command.

I have following structure of the object

 public class Inventory
        {
            public int Id { get; set; }
            public string SectionName { get; set; }
            public string SectionCode { get; set; }
            public double Size { get; set; }
         }

And below method adds the object in list

  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;           
        }

I have mentioned only 3 items in the list, in actual application there are many objects in list.

I have below code...

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

Now my issue is that, when the above command executes, its returning all the three records, I am expecting only one record which has "Section1" content... What is the issue ?

È stato utile?

Soluzione 2

I change my code for searching as -

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

and it gives me appropriate values as I want.

Altri suggerimenti

You are searching for empty strings on SectionCode and Size. Because you are using OR statements your query will match every object because of this. String.Contains will always return true when you search for empty string. See http://msdn.microsoft.com/en-us/library/dy85x1sa%28v=vs.110%29.aspx

Depending on your requirements, you can either ensure that ALL fields match your filters by changing your ORS to ANDS. This will result in every object needing to have every field match your filters.

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

Or if you'd like to only filter on the fields that are filled, in which case you should only filter on those fields which are filled using String.IsNullOrWhiteSpace. This will result in every object needing only one field to match a filter

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()));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top