Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

A SkipWhile question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ilyas - 10 Mar 2008 09:48 GMT
Hi all

I have the following code:

           List<int> items1 = new List<int>() { 1, 2, 3, 4, 5 };

           items1.SkipWhile(i => i % 2 == 1).ToList()));

this produces {2,3,4,5} I was epecting 2,4

Can anyone explain? - I'm trying to get al the even numbers
Jon Skeet [C# MVP] - 10 Mar 2008 10:20 GMT
> I have the following code:
>
[quoted text clipped - 5 lines]
>
> Can anyone explain?

From the docs:

<quote>
Bypasses elements in a sequence as long as a specified condition is
true and then returns the remaining elements.
</quote>

In other words, it bypasses just the first element (because the
condition is true), then returns the rest of the elements because the
condition is false for the second element.

> I'm trying to get al the even numbers

In that case you want to use Where, which applies a filter to *every*
element.

Take/TakeWhile/Skip/SkipWhile are all about returning/ignoring the
first part of the sequence, up to a particular point - where that point
is either specified by "the first element to fail a predicate" or an
index. After that, the rest of the sequence is ignored/returned.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Anthony Jones - 10 Mar 2008 10:26 GMT
Signature

Anthony Jones - MVP ASP/ASP.NET

> Hi all
>
[quoted text clipped - 7 lines]
>
> Can anyone explain? - I'm trying to get al the even numbers

From MSDN:-

Bypasses elements in a sequence as long as a specified condition is true and
then returns the remaining elements

So its doing what it says it does.  Note that it does not continue to test
the condition once it has been satisified.

What your looking for is a .Where(i => i % 2 == 1)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.