简体   繁体   English

ASP.NET MVC 3 LINQ中的结果集过滤

[英]resultset filtering in asp.net mvc 3 linq

Say i've a action of a controller like 说我有一个类似控制器的动作

   IList <post> p =db.posts.Include("user").ToList();


        if (Request.Form["searchString"] != null)          
        {
            if ((p!=null) && (p.Any()))

            {
                p =(p.Where(a=>a.area==Request 
                .Form["searchString"]).Tolist();                                
            }
       }           
        if (Request.Form["searchString2"] != null)          
        {              
            if ((p!=null) && (p.Any()))

            {


            p=(p.Where(a=>a.city==Request 
          .Form["searchString2"]).Tolist();                                            
            }
        }  
      return View(p);

here first i've get a resultset(p). 这里首先我有一个结果集(p)。 then i want to filter p by some criterias.it gives error for the ToList() inside the condition block. 然后我想通过一些标准过滤p。它为条件块内的ToList()提供了错误。 error: there is no definition for tolist() in the post 错误: 帖子中没有tolist()的定义

it would be: 这将是:

 if ((rs!=null) && (rs.Any()))
     {
     rs = (from posts1 in rs where                   
     posts1.area="Dhaka").ToList() ;                             
     }

just consider that if you call . 如果您致电,请考虑一下。 ToList() or Any it will go to DB (in case you are using a ORM) ToList()Any将进入数据库(如果您使用的是ORM)

var filteredRS=rs.Where(s=>s.area=="Dhaka").ToList();

You could filter directly: 您可以直接过滤:

List <student> rs = db.students.Where(student => student.area == "Dhaka").ToList();

or: 要么:

List <student> filtered = rs.Where(student => student.area == "Dhaka").ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM