您被捕获的变量咬了一口:
var query = color.Where(c => c.Contains(s));
升降机s
成闭合,并且读取的值s
在执行时.在这种情况下,这会在您重新分配s
给其他内容后发生.
你最终得到的是你的查询:
var query = color.Where(c => c.Contains(s)).Where(c => c.Contains(s));
而不是你可能期望的:
var query = color.Where(c => c.Contains("g")).Where(c => c.Contains("a"));
这将产生您期望的结果:
string s = "g"; string[] color = { "greena", "browna", "bluea" }; var query = color.Where(c => c.Contains(s)); Console.WriteLine(query.Count()); var b = "a"; query = query.Where(c => c.Contains(b)); Console.WriteLine(query.Count()); // <-- This is where the entire expression is evaluated