1

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

4
  • 4
    Your code should work. Show how you populate your foos variable. Commented Jan 7, 2021 at 11:33
  • I have added some loop output. Still the linq queries are empty Commented Jan 7, 2021 at 11:42
  • If any object have f.Type = null then your first Where(f => f.Type.Length <= 1) will throw exception. Moreover it should be < 1 not <= 1. Your second condition is perfect but what if your Type has value with spaces (" ") then it will not return those. You can use var emptyType1 = foos.Where(f => string.IsNullOrWhiteSpace(f.Type)); or use Trim() as var emptyType1 = foos.Where(f => f.Type == null || f.Type.Trim() == ""); Commented Jan 7, 2021 at 11:42
  • 1
    @user3732793: Are you sure that your second entry is acutally empty? Or does it have some whitespace character like space or tab? Commented Jan 7, 2021 at 11:45

1 Answer 1

3

This should cover almost every type of null/blank/just whitespace

var emptyType1 = foos.Where(f => String.IsNullOrWhiteSpace(f.Type));

but more likely what you want to do is exclude those - not include them

var dataFoos = foos.Where(f => !String.IsNullOrWhiteSpace(f.Type));

foreach (Foo f in dataFoos)
{
    Console.WriteLine(f.Id + f.Type);
}
Sign up to request clarification or add additional context in comments.

11 Comments

Side note: Any styling reason why you use String instead of string?
@Magnetron it's the same thing. One is just an alias of the other
@Jamiec that did work but than foos is empty I would hope I can generate other Lists too from foo !?
@user3732793 I'm afraid I don't understand. What other list do you want to generate?
@Magnetron habit only I guess. string is the c# type so I use that for defining params/vars etc. String has static methods like IsNullOr..... I know you can use either interchangably but it's just my personal taste
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.