简体   繁体   中英

LINQ where does not return expected values

Why is the following snippet not returning the expected result?

List<string[]> data
//filling list with some values (left here out to make problem more clear)

var allRowsHavingSomeWordWithLengthGreaterThanFive = (from d in data
                         from c in d
                         where c.Length > 5
                         select d);

data is a List containing an array which contains strings in each row.

This statement returns null.

What am i doing wrong?

The expression is perfect as it's. I don't know why it shouldn't work.

You could make it a little easier as:

var allRowsHavingSomeWordWithLengthGreaterThanFive = 
         from d in data
         where d.Any(q => q.Length > 5)
         select d;

But I don't see why.

Perhaps the problem is that there are null string s or null string[] ?

var allRowsHavingSomeWordWithLengthGreaterThanFive = (from d in data
                                                      where d != null
                                                      from c in d
                                                      where c != null && c.Length > 5
                                                      select d).ToArray();

See tester http://ideone.com/ci8zw1

Xanatos is right... It's about the Null values.. Should have noticed earlier. A simple null check is enough:

from d in data
from c in d
where c!=null &&  c.Length > 5
select d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM