过滤搜索linq查询 - mvc [英] Filter search linq query - mvc

查看:64
本文介绍了过滤搜索linq查询 - mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



下面的当前查询只会在搜索和类型过滤器同时使用时过滤结果。如何通过(名称),(类型)或(类型和名称)来获取查询以过滤数据。我是否需要在linq查询中添加谓词逻辑。我正在学习和编程,所以请原谅我的愚蠢错误或文盲逻辑。



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.

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







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.

推荐答案

小变化将诀窍

Small Change will do the trick
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

if (!String.IsNullOrEmpty(search))



to


to

if (!String.IsNullOrEmpty(type))


您可以编写查询代码如



You can write your query code like

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.


这篇关于过滤搜索linq查询 - mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆