Writing 'CONTAINS' query using LINQ
Asked Answered
R

1

8

Given the output of query:

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

What would you consider the neatest way to detect if a file is in the queryResult? Here is my lame try with LINQ:

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

There must be an more elegant way to figure out the result.

Roque answered 4/3, 2010 at 12:57 Comment(0)
B
17
string searchedFileName = "hello.txt";
var contains = queryResult.Any(file => file.FileName == searchedFileName);
Barbette answered 4/3, 2010 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.