Solved

Enumerable Skip

Posted on 2010-09-20
3
277 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now