0

I'm trying to use this linq query to check if certain object's netIds are null, blank, or otherwise empty.

var badData = FileSignatures.Drive.Public.Where(
    e => String.IsNullOrWhiteSpace(
        e.NetworkBlock?.Select(n => n.netId)) &&
         String.IsNullOrWhiteSpace(
             e.WiFiBlock?.netId) &&
             String.IsNullOrWhiteSpace(
                 e.BluetoothBlock?.netId))
    .ToList();                  

WiFiBlock and BluetoothBlock are both just single objects so the above should work.

However, NetworkBlock is an array of objects so I need to figure out how to iterate through each object in NetworkBlock and check it's netId.

Is it possible to do something like that inside a linq query?

Thanks!

1
  • e.NetworkBlock?.Any(n => String.IsNullOrWhitespace(n.Id)) or something of that nature. It's an enumeration; you can do anything with it in that lambda that you could anywhere else. Commented Oct 7, 2019 at 15:44

1 Answer 1

2

Sure thing!

change this:

String.IsNullOrWhiteSpace(
        e.NetworkBlock?.Select(n => n.netId))

to this:

e.NetworkBlock?.Any(n => String.IsNullOrWhiteSpace(n.netId)) ?? false // or true depending on criteria

or using All depending on your criteria.

full code:

var badData = FileSignatures.Drive.Public.Where(
    e => (e.NetworkBlock?.Any(n => String.IsNullOrWhiteSpace(n.netId)) ?? false) &&
         String.IsNullOrWhiteSpace(
             e.WiFiBlock?.netId) &&
             String.IsNullOrWhiteSpace(
                 e.BluetoothBlock?.netId))
    .ToList();    
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! That looks like it should work but I am getting "cannot convert from bool to string".
@SkyeBoniwell that shouldn't be with the code I've introduced. look carefully elsewhere in your code.
Oh ok, I know that 'false' is a boolean, so I thought it might have been trying to convert that to a string. It was because I forgot an open parentheses!

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.