无法将类型'System.Linq.IQueryable <Database.Table>'隐式转换为'bool'

Key*_*dly 2 c# wpf linq-to-sql

您好我收到错误

Cannot implicitly convert type 'System.Linq.IQueryable<Database.Table>' to 'bool'
Run Code Online (Sandbox Code Playgroud)

从这段代码

foreach (var file in files)
{
  if (context.SomeTables.Where(p => p.FileName == System.IO.Path.GetFileName(file)))
  {
      //Do Something     //above I am trying to compare if the filename in the db table
                         //is equal to the GetFileName being passed in but it throwing
                         //the error      
  }
Run Code Online (Sandbox Code Playgroud)

Rob*_*vey 14

context.SomeTables.Where(p => p.FileName == System.IO.Path.GetFileName(file))
Run Code Online (Sandbox Code Playgroud)

返回一个IQueryable,而不是一个bool.你需要能够返回的东西bool,比如

context.SomeTables.Any(p => p.FileName == System.IO.Path.GetFileName(file)) 
Run Code Online (Sandbox Code Playgroud)

作为if陈述的条件.

请参见
Enumerable.Any方法(IEnumerable)

  • `Count()`在这里会很浪费,因为它会迭代每一个匹配,而不是仅仅查看是否有一个然后停止,"Any()"会做什么. (2认同)
  • @cdhowie:谢谢. (2认同)