TakeWhile LINQ method not giving result as expected

I am expecting the string which has "i" but getting empty results. Can you tell me the reason?

PetOwner[] petOwners = { new PetOwner { Name = "sen", Pets = new List { "puppy", "tiger" } }, new PetOwner { Name = "sugu", Pets = new List { "jimmy", "rose" }} };

        var pets = petOwners.SelectMany(p => p.Pets);

        var pets1 = pets.TakeWhile<string>(s => { Console.WriteLine(s); return s.Contains("i"); });

Answers


Don't use TakeWhile for this - it terminates the loop as soon as it encounters an element for which the expression returns false. Use Where instead. Also just use an ordinary foreach loop to do the output instead of putting the call to WriteLine inside the lambda function. This makes it much easier to understand your code.

var petsContainingI = petOwners.SelectMany(p => p.Pets).Where(s => s.Contains("i"));
foreach (string s in petsContainingI)
{
    Console.WriteLine(s);
}

Need Your Help

SVN 1.6 - not a valid URL

macos svn

Recently I uninstalled SVN 1.7 from mac osx and installed SVN 1.6.23 following the instructions on this page: How do I uninstall subversion on OS X

How to sort through a list of observable coordinates?

python coordinates astronomy astropy

I am struggling to find the best way of removing unwanted targets from a list of coordinates. My coordinates (Ra, Dec) are formed using astropy.coordinates.SkyCoord but I have a large number of un-