使用LINQ编写'CONTAINS'查询

时间:2010-03-04 12:57:50

标签: c# .net linq code-formatting

给出查询的输出:

var queryResult = from o in objects
                  where ...
                  select new 
                      {
                         FileName = o.File,
                         Size = o.Size
                      }

您认为检测文件是否在queryResult中的最佳方法是什么? 这是我对LINQ的蹩脚尝试:

string searchedFileName = "hello.txt";
var hitlist = from file in queryResult
              where file.FileName == searchedFileName
              select file;
var contains = hitlist.Count() > 0;

必须有更优雅的方法来确定结果。

1 个答案:

答案 0 :(得分:17)

string searchedFileName = "hello.txt";
var contains = queryResult.Any(file => file.FileName == searchedFileName);