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

标签 c# wpf linq-to-sql

你好,我遇到了错误

Cannot implicitly convert type 'System.Linq.IQueryable<Database.Table>' to 'bool'

来自这段代码

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      
  }

最佳答案

context.SomeTables.Where(p => p.FileName == System.IO.Path.GetFileName(file))

返回一个IQueryable,而不是一个bool。你需要一些返回 bool 的东西,比如

context.SomeTables.Any(p => p.FileName == System.IO.Path.GetFileName(file)) 

作为 if 语句的条件。

另见
Enumerable.Any Method (IEnumerable)

关于c# - 无法将类型 'System.Linq.IQueryable<Database.Table>' 隐式转换为 'bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17094894/

相关文章:

c# - 按钮看起来已禁用,直到我单击某些内容

c# - 如果使用可为空的 int 变量,LINQ 返回 0 个结果,如果使用 "null",则返回准确的结果

c# - 如何在此 lambda 表达式中指定 "not in"?

c# - 如何在 Windows 商店应用程序中截取屏幕截图

c# - 使用 Expression<Func<...>> 的 linq to sql 自定义方法

wpf - WPF应用的帧率限制?

c# - WPF - 通过 TableAdapter、DataContext/Dataview 和 DataGrid 将数据库编辑到 TextBox

asp.net - Linq 更新查询生成 Where 0 = 1?

c# - 从数据库中检索主键

c# - 将类的成员重构为结构容器时是否需要考虑性能?