简体   繁体   English

LINQ不返回期望值的地方

[英]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[] ? 也许问题在于存在null stringnull 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 参见测试仪http://ideone.com/ci8zw1

Xanatos is right... It's about the Null values.. Should have noticed earlier. Xanatos是正确的...它与Null值有关。应该早一点注意到。 A simple null check is enough: 一个简单的空检查就足够了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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