Solved

Enumerable Skip

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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