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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:42:39+00:00 2026-05-23T16:42:39+00:00

Is this faster var query = from prop in object.GetType().GetProperties() where prop.Name == Id

  • 0

Is this faster

var query = from prop in object.GetType().GetProperties()
            where prop.Name == "Id"
            select prop;

var singleProperty = query.SingleOrDefault();
//do stuff with singleProperty

than this?

var type = object.GetType();
foreach(var prop in type.GetProperties())
{
  if(prop.Name == "Id")
   {
     //do stuff
   }
}

The other way around? Or are they the same?
Why and how would you know?

Sorry to be overly direct in my question. I prefer the first one but I don’t know why or if I should.

Thanks.

  • 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-23T16:42:40+00:00Added an answer on May 23, 2026 at 4:42 pm

    Technically, the first case may allocate more memory and do more processing to generate the final result than the second case because of the intermediate data and LINQ abstractions. But the amount of time and memory is so negligible in the grand scope of things, that you’re way better off making your code the most readable than the most efficient for this scenario. It’s probably a case of premature optimization.

    Here are some references why the first may be slightly slower:

    1. http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html
    2. http://ox.no/posts/linq-vs-loop-a-performance-test
    3. http://geekswithblogs.net/BlackRabbitCoder/archive/2010/04/23/c-linq-vs-foreach—round-1.aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To put it simply: Is this: var x = $('#selector-id'); x.slideDown(); Faster than: var
Is this faster: $(document.links).filter('a.someClass') than just plain old this: $('a.someClass') ? I don't see
This consists of two questions: Is MySQL's timestamp field really faster than datetime field
Switch statements are typically faster than equivalent if-else-if statements (as e.g. descibed in this
Hey there - simple query: var q = (from SomeObject o in container where
string[] words = System.IO.File.ReadAllLines(word.txt); var query = from word in words where word.Length >
Is there anyway to make this SQL query faster? It searches a .sdf database
I have an object that looks like this: var obj = { a: text,
Given this linq query against an EF data context: var customers = data.Customers.Where(c =>
I see a lot of people doing this Object.prototype.foo = 'HALLO'; var hash =

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.