Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:50:13+00:00 2026-05-28T22:50:13+00:00

I am using the new mvc4 ajax controller template and I don’t know how

  • 0

I am using the new mvc4 ajax controller template and I don’t know how to add a search to the database query that they have.

Let’s say I have a model called Article, here is what the controller contains by default:

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

This works fine, but I am not familial with IObjectContextAdapter and I don’t know how to add a search (ie. to see if a Article contains a term.) Normally I would have something like 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":
     ...
}

Obviously the new way of doing this is more succinct so I would like to stick with this. I tried combining them, using the linq query to populate the var articles, but then I am not able to use the order by part, .OrderBy(“it.” + orderBy + (desc ? ” desc” : “”)).

So my question is what is the best way to search through my database? And what exactly is this .OrderBy(“it.” + orderBy + (desc ? ” desc” : “”)) doing? I’m only familial with using OrderBy with linq.

Edit

After a lot of reading I have a better understanding of what is going on, although I still can’t get this to work.

According to this it seems like I need something similar to this:

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

although I think this would only work if searchString matched the title exactly. While I still don’t quite understand what this is doing, I think it uses sql expressions so I thought that this may work:

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

But none of this worked, and I am only shooting in the dark since I am unfamiliar with this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:50:13+00:00Added an answer on May 28, 2026 at 10:50 pm

    The most succinct way to query your model without changing mvc4 template code is to create an IQueryable object and query it, like 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 the entries in the correct order, and be sure to return the new iArticles instead of the old Articles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the new ASP.Net ListView control to list database items that will be
I came to know that using Reflection we can create objects without using new
HTML5 will be next super star. So~~~How to using new idea to implement AJAX
i am using new to prototype and using its ajax functionality but not able
I created a CultureInfo object using new CultureInfo(fr-FR). Now I have a number that
I have the following code that I have tested and works: using (new Impersonator(Administrator,
Let's say I want to make an iOS application that is using new notifications
I knew that when we allocate memory by using new/new[], then we should release
I'm extending DateTime do add some useful methods and constants. When using new to
I am starting a new project using MVC4 and would like to use dependency

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.