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 7434715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:56:22+00:00 2026-05-29T09:56:22+00:00

I have this controller method: public ActionResult Index(string searchError) { // get all errors

  • 0

I have this controller method:

public ActionResult Index(string searchError)
{
    // get all errors
    var viewModel = _errorsRepository.Errors.OrderByDescending(e => e.TimeUtc).
                            Select(e => new ErrorViewModel
                                            {
                                                ErrorId = e.ErrorId,
                                                Message = e.Message,
                                                TimeUtc = e.TimeUtc
                                            });

    if (!String.IsNullOrEmpty(searchError))
            viewModel = viewModel.Where(e => e.Message.ToLower().Contains(searchError.Trim().ToLower()));

    return View(viewModel);
}

I think doing the extra filter is slowing everything down, I was wondering if I can add the Where clause to the Select statement and check to see if searchError is null inline.

Is this possible?

  • 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-29T09:56:23+00:00Added an answer on May 29, 2026 at 9:56 am

    Since Linq is lazy it does not matter if you have “one big statement” or multiple, as long as you don’t execute your query (e.g. by iterating over the results or forcing eager execution using ToList()) there is no penalty since you are just chaining extension methods. In this regard I would focus on readability.

    There are things to consider though, e.g. sorting cannot be lazy (you have to look at all items before you can spit out the items in order) – that’s why you should always put your Where filter before your OrderBy so you have less items to sort. This said I would restructure your code like this:

    // get all errors
    var viewModel = _errorsRepository.Errors;
    
    // optionally filter            
    if (!String.IsNullOrEmpty(searchError))
    {
        string searchErrorMatch = searchError.Trim().ToLower();
        viewModel = viewModel.Where(e => e.Message.ToLower().Contains(searchErrorMatch));
    }
    
    //order and project to ErrorViewModel
    viewModel = viewModel.OrderByDescending(e => e.TimeUtc)
                         .Select(e => new ErrorViewModel
                          {
                              ErrorId = e.ErrorId,
                              Message = e.Message,
                              TimeUtc = e.TimeUtc
                          }).ToList();
    

    Also note that I pulled out searchError.Trim().ToLower() out of your lambda and assigned it to a variable once – otherwise this is executed every time the lambda is evaluated, which is really unnecessary work.

    Final edit: I also added a ToList() to execute the query after your projection – otherwise your query really would be executed from your view which in general is bad for many reasons, e.g. you have to keep the database context alive for a potentially much longer time and you are violating separation of concerns – views should only be concerned about the view model but have nothing to do with acquiring the data.

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

Sidebar

Related Questions

I have a controller action like this: public ActionResult Index(string url) { var pageTitle
I have a controller method that looks like this: [AcceptGet] public ActionResult Index(SecurityMatrixIndexViewModel model)
I have an action method that looks like this: public ActionResult Index(string message) {
i have this controller: public ActionResult Novo() { var products = context.Product.Select(x => new
I have 2 Action methods in one controller, Index: public ActionResult Index(string url) {
On ASP.NET MVC 3, assume that we have following controller action: public ActionResult Index()
I have a controller method with a custom FilterAttribute on it... [ActivityHistory] public ActionResult
I am getting some unexpected behavior from Html.EditorFor(). I have this controller: [HandleError] public
I have a controller called MetricsController with a single action method: public class MetricsController
I have an action in my controller as follows. // // GET: /HostingAgr/Details/1 public

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.