Solved

Enumerable Skip

Posted on 2010-09-20
3
283 Views
Last Modified: 2013-11-11
I have two codes as follow,

Code 1
           int[] grades = { 1, 2, 3, 4, 5 };

            var q = from c in grades.Skip(1)
                    select c;

            foreach (int number in q)
                Console.WriteLine("{0}", number);

Open in new window


Code 2
            int[] grades = { 1, 2, 3, 4, 5 };

            var q = from c in grades
                    where c > 1
                    select c;

            foreach (int number in q)
                Console.WriteLine("{0}", number);

Open in new window


They provide the same result.
If the list contains billion values and filter condition is different, base on the performance which one is better?
0
Comment
Question by:mawingpui
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 166 total points
ID: 33717728

Code 1 which uses Skip would be faster as Code 2's 'where c > 1' is checked on every item.
0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 167 total points
ID: 33723781
I don't think there will be any performance difference between the two.

The third way would be - var q = grades.Where(x => x > 1);

a more simpler notation though.

I can only recommend you to test to see the execution time required in all 3 approaches and decide which one is better suitable for you.
0
 
LVL 3

Assisted Solution

by:CyberSoft
CyberSoft earned 167 total points
ID: 33769044
There are actually two fundamental differences in the logic of the two code-snippets above.

Code 1: Ignores the first record in the result set (regardless of value)

Code 2:: Excludes values from the result set where the grade is less than or equal to 1

If there is ordering involved or other records with the same values as in the where clause you'll get two distinctly different result sets which means you may not necessarily get the same results from both queries.

HTH
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question