Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple Listbox filters with select all at Linq

I have 2 listbox like below. I want to filter the list from the listbox parameters, forexample segment A and All Status. What should I write into the ALL value in the Listbox?

<select class="form-select form-select-solid" name="Segment">
   <option value="???">All</option>
   <option value="A">Segment A</option>
   <option value="B">Segment B</option>
   <option value="C">Segment C</option>
</select>
<select class="form-select form-select-solid" name="Status" >
   <option value="???">All</option>
   <option value="Lead">Lead</option>
   <option value="Active">Active</option>
   <option value="Passive">Passive</option>
</select>

This is the JsonResult query:

public JsonResult GetFilteredList(string segment, string status) {
var JsonList = from c in db.TblAccount.Where(x=>x.Segment == segment && x.Status = status)
    select new { id = c.RecId, name = c.Name, status = c.status, segment = c.Segment };

return Json(new { data = JsonList }, JsonRequestBehavior.AllowGet);
}

I try to send "" or " " or "*" but don’t work?
What is the method to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is common behavior that most web applications use. Here are a couple of tips when working in C#, using LINQ queries:

  1. Dont mix LINQ expressions
  2. Only enumerate once
  3. Keep the enumerations simple
  4. Use object equality
var JsonList = db.TblAccount
    .Where(x => 
        (segment.Equals("*") || segment == x.Segment) &&
        (status.Equals("*") || status == x.Status)
    )
    .Select(x => ...);

We first always check if segment is equal to *, if it is, then case one is met. Then we check if status is equal to *. If it is, then case two is met.

Otherwise, either the segment and status must match the item in the enumerable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading