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

  • Home
  • SEARCH
  • 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 681411
In Process

The Archive Base Latest Questions

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

I was wondering if i extract the common where clause query into a common

  • 0

I was wondering if i extract the common where clause query into a common expression would it make my query much faster, if i have say something like 10 linq queries on a collection with exact same 1st part of the where clause.

I have done a small example to explain a bit more .

public class  Person
{
    public string First { get; set; }
    public string Last { get; set; }
    public int Age { get; set; }
    public String Born { get; set; }
    public string Living { get; set; }
}

public sealed class PersonDetails : List<Person>
{
}



PersonDetails d = new PersonDetails();
        d.Add(new Person() {Age = 29, Born = "Timbuk Tu", First = "Joe", Last = "Bloggs", Living = "London"});
        d.Add(new Person() { Age = 29, Born = "Timbuk Tu", First = "Foo", Last = "Bar", Living = "NewYork" });

        Expression<Func<Person, bool>> exp = (a) => a.Age == 29;
        Func<Person, bool> commonQuery = exp.Compile();

        var lx = from y in d where commonQuery.Invoke(y) && y.Living == "London" select y;

        var bx = from y in d where y.Age == 29 && y.Living == "NewYork" select y;

        Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}", lx.Single().Age, lx.Single().First , lx.Single().Last, lx.Single().Living, lx.Single().Born );
        Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}", bx.Single().Age, bx.Single().First, bx.Single().Last, bx.Single().Living, bx.Single().Born);

So can some of the guru’s here give me some advice if it would be a good practice to write query like

 var lx = "Linq Expression "

or

 var bx = "Linq Expression" ?

Any inputs would be highly appreciated.

Thanks,
AG

  • 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-14T01:28:50+00:00Added an answer on May 14, 2026 at 1:28 am

    Firstly, Eric’s absolutely right: if you’re concerned about performance, you need to measure it. Work out exactly what you want to measure, and record what happens for each change you make in your code. There are various aspects of benchmarking which I don’t have time to go into now, but the key one is probably to make sure you run the tests for long enough for them to be meaningful – if your test only takes 50ms, you’re unlikely to be able to tell improvements in your code from noise.

    Now, if you’re using LINQ to Objects then you almost certainly don’t want to be using expression trees at all. Stick to delegates – that’s what LINQ to Objects uses anyway.

    Now, as for restructuring… if you’ve got a common predicate, then you can filter your list by that to start with, to come up with a new IEnumerable<T>. The predicate will be applied lazily, so it won’t make any difference to execution speed, but it might make your code more readable. It could slow things down very slightly as it will introduce an extra level of indirection when you’ve effectively got two different where clauses.

    If the result of applying the filters will have very few results, you may want to materialize it (e.g. by calling ToList) and remember the result – that way you don’t need to query the whole thing again for the second query.

    However, the big benefit that I can see would be from only calling Single once for each query. Currently you’re executing the whole query for every single property – that’s clearly inefficient.

    Here’s your code, rewritten accordingly – and using a collection initializer too:

    PersonDetails d = new PersonDetails
    {
        new Person {Age = 29, Born = "Timbuk Tu", First = "Joe", 
                   Last = "Bloggs", Living = "London"},
        new Person { Age = 29, Born = "Timbuk Tu", First = "Foo", 
                    Last = "Bar", Living = "NewYork" }
    };
    
    var peopleOfCorrectAge = d.Where(a => a.Age == 29);
    
    var londoners = peopleOfCorrectAge.Where(p => p.Living == "London");
    var newYorkers = peopleOfCorrectAge.Where(p => p.Living == "New York");
    
    var londoner = londoners.Single();
    var newYorker = newYorker.Single();
    
    Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}",
                      londoner.Age, londoner.First, 
                      londoner.Last, londoner.Living, londoner.Born);
    
    Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}",
                      newYorker.Age, newYorker.First, 
                      newYorker.Last, newYorker.Living, newYorker.Born);
    

    Alternatively, for the last section, encapsulate the “writing out a single person”:

    DisplayPerson(londoners.Single());
    DisplayPerson(newYorkers.Single());
    
    ...
    
    private static void DisplayPerson(Person person)
    {
        Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}",
                          person.Age, person.First, 
                          person.Last, person.Living, person.Born);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.