is it possible in Linq to select from IEnumerable of this object
public class Foo
{
public int Id { get; set; }
public string Type { get; set; }
}
where Type is "" ?
if I loop over the list with that
foreach (Foo f in dataFoos)
{
Console.WriteLine(f.Id + f.Type);
}
it looks like
1one
2
3three
I have tried
var emptyType0 = dataFoos.Where(f => f.Type.Length <= 1);
var emptyType1 = dataFoos.Where(f => f.Type == null || f.Type == "");
both did not return any result. Any hint on how to properly check if String values are empty ?
if I do that
var df = dataFoos.Where(f => String.IsNullOrWhiteSpace(f.Type));
foreach (Foo f in df)
{
Console.WriteLine(f.Id + f.Type);
}
var df1 = dataFoos.Where(f => !String.IsNullOrWhiteSpace(f.Type));
foreach (Foo f in df1)
{
Console.WriteLine(f.Id + f.Type);
}
the second loop does not return any value
I am using dotnetcore c#. Thanks for any hint
foosvariable.f.Type = nullthen your firstWhere(f => f.Type.Length <= 1)will throw exception. Moreover it should be< 1not<= 1. Your second condition is perfect but what if yourTypehas value withspaces (" ")then it will not return those. You can usevar emptyType1 = foos.Where(f => string.IsNullOrWhiteSpace(f.Type));or useTrim()asvar emptyType1 = foos.Where(f => f.Type == null || f.Type.Trim() == "");