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

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?

>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.

Leave a ReplyCancel reply