I'm while using new mvc4 ajax controller template and I'm not sure how you can give a search towards the database query they have.

Let us say I've got a model known as Article, here's what the controller consists of automatically:

ObjectQuery<Article> articles = (db as IObjectContextAdapter).ObjectContext.CreateObjectSet<Article>();
articles = articles.OrderBy("it." + orderBy + (desc ? " desc" : ""));

This works fine, but I'm not familial with IObjectContextAdapter and I'm not sure how you can give a search (ie. to ascertain if a Article consists of a phrase.) Normally I'd have something similar to this:

var articles = from a in db.Articles.ToList()
                           select a;
if(!String.IsNullOrEmpty(search))
{
    articles = articles.Where(a => a.Title.ToLower().Contains(search.ToLower())
                                       || a.FullArticle.ToLower().Contains(search.ToLower())
                                       || a.TagsAndKeywords.ToLower().Contains(search.ToLower()));
}
switch(orderBy)
{
    case "TimeStamp":
        if(desc)
            articles = articles.OrderByDescending(a => a.TimeStamp);
        else
            articles = articles.OrderBy(a => a.TimeStamp);
            break;
     case "Title":
     ...
}

Clearly the brand new method of carrying this out is much more succinct so I must stick to this. I attempted mixing them, while using linq query to populate the var articles, however I'm not able to utilize an order by part, .OrderBy("it." + orderBy + (desc ? " desc" : "")).

So my real question is what's the easiest method to sort through my database? And what is this .OrderBy("it." + orderBy + (desc ? " desc" : "")) doing? I am only familial with using OrderBy with linq.

Edit

After lots of reading through I've got a better knowledge of what's going on, although I still can't get this work.

Based on this it appears like I want something such as this:

articles = articles.Where("it.Title = @searchString");

although I believe this could only work if searchString matched up the title exactly. As I still don't quite know very well what this really is doing, It uses sql expressions and so i believed that this might work:

articles = articles.Where("it.Title like '%@searchString%'");

But none of them of the labored, and i'm only shooting at nighttime since i have am not really acquainted with this.

Probably the most succinct method to query your model without altering mvc4 template code would be to create an IQueryable object and query it, such as this:

ObjectQuery<Article> articles = (db as IObjectContextAdapter).ObjectContext.CreateObjectSet<Article>();
articles = articles.OrderBy("it." + orderBy + ((bool)desc ? " desc" : ""));

IQueryable<Article> iArticles = articles;

if(!String.IsNullOrEmpty(search))
    iArticles = iArticles.Where(a => a.Title.Contains(search) ||
                                                  a.FullArticle.Contains(search));

Response.AppendHeader("X-Total-Row-Count", iArticles.Count().ToString());

return PartialView(iArticles.Skip(start).Take((int)itemsPerPage));

This keep your records within the correct order, and make certain to come back the brand new iArticles rather than that old Articles.