A rejoint le : 14 mai 2022

À propos

Kunci Jawaban Advance Accounting Edisi 9 Floyd A Beams flegitt




Beams Category:Conglomerate companies of the United StatesQ: IEnumerable.First and Where extension method I am working with LINQ and IEnumerable. I have the following question: string myString = "Hello"; string[] myArray = { "One", "Two", "Three" }; string result = myString.Where(x => myArray.Contains(x)); Console.WriteLine(result); // prints "One" Why is result prints One rather than printing null? I am asking this because the IEnumerable.First and the Where extension method should both return an object. A: Enumerable.Where returns an IEnumerable. If you call First on that IEnumerable without specifying the overload that will take a Func, it returns the first item from the sequence. If you want to use First and Where, you can do something like this: IEnumerable strings = Enumerable.Range(1, 5) .Select(n => "foobar" + n) .ToArray(); var myArray = strings.Select(x => x.Split('.')[0]) .ToArray(); Console.WriteLine(strings.First(x => myArray.Contains(x))); Console.WriteLine(strings.Where(x => myArray.Contains(x)).First()); The above example prints out: foobar1 foobar2 You'll notice that it prints out two different results because the first item isn't the only one that matches the condition, but the First method returns the first of that sequence. If you wanted to use First and Where and still take advantage of the IEnumerable.Where method, you could do something like this: string[] myArray = { "




Kunci Jawaban Advance Accounting Edisi 9 Floyd A Beams flegitt

Plus d'actions