Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear all,

The current query below, will only filter result if both search and type filters are used together. How can I get the query to filter data by (name),or (type),or (type and name). Would I need to add predicate logic in the linq query. I am currently learning and programming as I go along, so please excuse my silly mistakes or illiterate logic.

C#
public ActionResult Search(string search, string type)
 {

     var TypeList = new List<string>();

     var TypeQry = from d in db.data_qy
                    orderby d.Type
                    select d.Type;

     TypeList.AddRange(TypeQry.Distinct());

     ViewBag.type = new SelectList(TypeList);


     DateTime yesterday = DateTime.Today.AddBusinessDays(-1);

     var query = from s in db.data_qy
                where s.UploadDate == yesterday
                select (s);


     if (!String.IsNullOrEmpty(search))
     {
         query = query.Where(s => s.Name.Contains(search));
     }

     if (!String.IsNullOrEmpty(search))
     {
         query = query.Where(s => s.Type == type);
     }

     var data = query.ToList();

     return View(data);
 }


C#
odel IEnumerable<System_Search_May2014.data_qy>
@{
    ViewBag.Title = "Search";
}

<h2>Search</h2>
<p>
    @Html.ActionLink("Create New", "Create")
    @using (Html.BeginForm("Search", "Home", FormMethod.Get))
    {
    <p>
        Type: @Html.DropDownList("type", "All")
        Name: @Html.TextBox("search",ViewBag.FilterValue as string)
        <input type="submit" value="Filter" />
    </p>
    }
</p>


<table style="width: auto; padding: 10px;">
    <tr style="background-color: lightgray; padding: 10px;">
        <th>Name
        </th>
        <th>Type
        </th>
        <th>Cover
        </th>
    </tr>
 
    @foreach (var item in Model)
    {
        <tr>
            <td>
                 @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Type)
            </td>
              <td>
                @Html.DisplayFor(modelItem => item.Cover)
            </td>
           
        </tr>
    }
    </table>

Please advice, if possible.
Thank you in advance.
Posted

Small Change will do the trick
C#
if (!String.IsNullOrEmpty(search))
{
    query = query.Where(s => s.Name.Contains(search));
}

if (!String.IsNullOrEmpty(type))
{
    query = query.Where(s => s.Type == type);
}


Filter data
- by (name) : type will be empty then only the filter by name apply
- or (type) : name will be empty then only the filter by type apply
- or (type and name) : both filters will apply

Note that I have changed your 2nd condition
C#
if (!String.IsNullOrEmpty(search))

to
C#
if (!String.IsNullOrEmpty(type))
 
Share this answer
 
v2
Comments
miss786 12-May-14 6:40am    
Thank you for your quick response. Apology for the silly mistake and thanks a lot for pointing this out. Many thanks for your time and help. :)
DamithSL 12-May-14 6:42am    
Nop :)
You can write your query code like

C#
var result = from s in db.data_qy
	     where s.Name.Contains(search) && s.Type == type 
             && s.UploadDate == yesterday
             select s;


You can add more filters & for different type of search you have to write different queries.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900