[英]Linq Statement Difference.
有人可以解释其中的区别。 我试图避免文件夹中的隐藏文件并获得计数。 1语句不起作用,但是第二条语句起作用。 我不知道为什么。 LINQ中“&”的用法是什么。 提前致谢!
subFolder.GetFiles().Select(Function(k) k).Where(Function(m) m.Attributes <> FileAttributes.Hidden).Count
subFolder.GetFiles().Select(Function(k) k).Where(Function(m) (m.Attributes And FileAttributes.Hidden) = 0).Count
m.Attributes是一个[Flags]
枚举,表示值中的每个位代表一个不同的标志。 当您执行<>
您正在测试“隐藏的值不是唯一设置的吗?” 这对于许多文件都是正确的。 当您执行& == 0
您正在测试“仅查看隐藏位,该值是否设置为0?换句话说,是否未设置隐藏标志?”
m.Attributes
可以包含一些标志值的组合,而FileAttributes.Hidden
是其中之一(完整列表可以在MSDN中看到)
因此,第一个语句的谓词:
m.Attributes <> FileAttributes.Hidden
测试“不是( m.Attributes
仅由一个标志Hidden
)”,对于Hidden
和ReadOnly
或Hidden
和System
等组合的值,甚至都适用。
第二条陈述:
(m.Attributes And FileAttributes.Hidden) = 0
测试“从m.Attribute
拾取Hidden
的部分值,然后查看是否设置”,至少在该值未设置Hidden
且其他标志被忽略的情况下才成立。 因此,对于Hidden
和ReadOnly
或Hidden
和System
等的组合,这是错误的。
附带说明, .Select(Function(k) k)
不执行任何操作,可以省略。
subFolder.GetFiles().Where(Function(m) (m.Attributes And FileAttributes.Hidden) = 0).Count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.