17

我有一个查询应该返回 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;}
   }
   } 

MainViewModel 类

      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;
     }
     }
4

5 回答 5

34

尝试这个,

 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
于 2013-04-12T07:28:46.163 回答
5

如果我对您的理解正确,您想知道true查询的结果之一是否符合所有条件。在这种情况下,尝试这样的事情:

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

this.result = found.ToString();
于 2013-04-12T07:30:07.023 回答
4

您可以使用 .Any() 或 .Count()。Any()表现更好。(检查这个SO question看看为什么)

。任何()

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 = query.Any().ToString()

。数数()

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 = (query.Count() > 0).ToString()
于 2013-04-12T07:29:08.740 回答
0
var query = from c in db.Emp
            from d in db.EmpDetails 
            select new { c.ID == y.ID &&  
                         c.FirstName == "A" && 
                         c.LastName == "D" };
于 2013-04-12T07:25:23.160 回答
0

如果你真的想要一个“真”或“假”的字符串响应:

    public string result
    {
        get
        {
            return db.Emp.SelectMany(c => db.EmpDetails, (c, d) => new {c, d})
                         .Where(@t => c.ID == y.ID && c.FirstName == "A" && c.LastName == "D")
                         .Select(@t => c)).Any().ToString();
        }
    }

但我建议将属性“结果”设为布尔值并删除 ToString()。一旦你有一个布尔值,你总是可以在它上面做一个 ToString()

于 2013-04-12T08:08:54.507 回答