c# - Linq 查询返回 true 或 false

标签 c# linq linq-to-sql

我有一个查询,它应该返回 TRUE 或 FALSE。

var query = from c in db.Emp
            from d in db.EmpDetails 
            where c.ID == d.ID && c.FirstName == "A" && c.LastName == "D" 
          // It should return TRUE when this above statement matches all these conditions

我想将这个查询结果附加到一个属性(字符串数据类型)

this.result = Conert.ToBoolean(query);

如何在 LINQ 中实现这一点?

编辑:

EmpMapper 类

  public class EmpMapper
  {
    EmpEntities db;
    // ID column already exists in the DB
    private int ID;
    // I am creating this property to add it from the UI side, depending on the certain conditions in the query. That is why I created a separate class to map the existing ID from the DB
    bool result;
    public EmpMapper(int ID, bool result)
    {
      this.db = new EmpEntites();
      this.ID = ID;
      var query = from c in db.Emp
            from d in db.EmpDetails 
            where c.ID == d.ID && c.FirstName == "A" && c.LastName == "D" 
          // It should return TRUE when this above statement matches all these conditions
      this.result = Convert.ToBoolean(query);
    }
   public int ID
   {
    get{return this.ID;}
    set{this.ID = value;}
   }
   public bool result
   {
    get{return this.result;}
    set{this.result = value;}
   }
   } 

主视图模型类

      List<EmpMapper> empMapCol = new List<EmpMapper>();

     private void Page_Loaded(object sender, RoutedEventArgs e)
    {
      var emp_query = from c in db.Emp
                      orderby c.ID
                      select a;
     List<Emp> empCol = emp_query.ToList();
     foreach(Emp item in empCol)
     {
       this.empMapCol.Add(new EmpMapper(item.ID, item.result)); 
     }
     datagrid1.ItemsSource = empMapCol;
     }
     }

最佳答案

试试这个,

 var query = (from c in db.Emp
        from d in db.EmpDetails 
        where c.ID == d.ID && c.FirstName == "A" && c.LastName == "D"
         select c 
         ).Any(); 

  this.result = query; //no need to convert to boolean its already bool value

关于c# - Linq 查询返回 true 或 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15965423/

相关文章:

c# - 如何使用 linq 从嵌套字典中选择所有值?

linq-to-sql - linq2sql 缺少事件模型?

c# - 实体 - Linq to Sql 只加载实体的一部分

C# "using" block

c# - 如何从 C# 中的 bytearray 中读取一系列字节

c# - 使用 C# 扫描具有向下钻取功能的驱动器?

c# - Entity Framework ,高效的 NavigationProperty.OfType 查询

linq - 拆分为 KeyValuePair 而不是 Array

c# - 无法使用 "IN"时将 SQL 转换为 LINQ 查询

c# - 为什么每个 [TestMethod] 都会多次调用 [TestClass] 的构造函数?