Charl Potgieter Charl Potgieter - 3 months ago 18
C# Question

Use second element of a split string in LINQ where query

Is the following possible: Retrieve all last name that starts with a p using LINQ. But the first name and last name is one word separated by a space.
Example would be Jonh Peter. Luke Sam, Rock Papers,Lee P

I tried the following:

var result = test.Where(x => x.Owner.Split(' ')[1]
.ToLowerInvariant()
.StartsWith("p")
);

Answer

or just

var result = test.Where(x => x.IndexOf(" P", StringComparison.OrdinalIgnoreCase) > 0);