Solved

Enumerable Skip

Posted on 2010-09-20
3
274 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
Comment Utility

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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

12 Experts available now in Live!

Get 1:1 Help Now