sql server - How to search a column in a given database table using Entity-framework? -


i wanted provide site search feature. how can search string in table?

public class searchcontroller : controller {     hoxatentities db = new hoxatentities();     public actionresult index(string search, int? page)     {         var pagenumber = page ?? 1;         viewdata["posts"] = db.posts.topagedlist(pagenumber, 5);         viewdata["search"] = search;         return view();     } } 

the search string passed , needs compared or whatever entity

you use:

var posts = db.posts.where(x => x.nameofpost == search); return view(posts.topagedlist(pagenumber, 5)); 

if want search name of post. otherwise change whatever attribute of post want search out. can pop .tolist() on first line above becomes:

var posts = db.posts.where(x => x.nameofpost == search).tolist() 

but notice posts match search loaded memory list can populated , ready use. if don't want load list used first example returned type iqueryable.

then can alter view expect paged list of posts model , can render view based on this.


Comments