19

I have a query where it should return TRUE or 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

and I want to attach this query result to a property(of string datatype)

this.result = Conert.ToBoolean(query);

how to achieve this in LINQ ?

EDIT:

EmpMapper class

  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 class

      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;
     }
     }
Indhi
  • 1,684
  • 5
  • 27
  • 48
  • 1
    `query` will return one value *per row*. What would you want the result of calling `ToString` to be? – Jon Skeet Apr 12 '13 at 07:26
  • Are you looking for something like `(query.Count() > 0).ToString()`? – Alexei Levenkov Apr 12 '13 at 07:28
  • @jon:i guess he expects a true/false as a string – Prabhu Murthy Apr 12 '13 at 07:28
  • @CodeIgnoto: But that makes no sense - what if it's true for one result and false for another? – Jon Skeet Apr 12 '13 at 07:29
  • I would like to add this result to my DatagridTextcolumn, if the result is True I will display my cell as green & if false display it as red – Indhi Apr 12 '13 at 07:29
  • 2
    @user1221765: How are you intending to add it? Calling `ToString()` on a sequence definitely isn't going to help you. – Jon Skeet Apr 12 '13 at 07:30
  • @Jon - how can I add this result then ? can I set the result property to bool ? – Indhi Apr 12 '13 at 07:32
  • 1
    Consider to show slightly more code... Side note - "using foreach() to iterate through my class" is very interesting statement - not even sure what it would be. – Alexei Levenkov Apr 12 '13 at 07:33
  • Fundamentally, there isn't nearly enough detail/context in this question to answer is sensibly. You've got a lot of information you're not telling us, or telling us in a drip-drip fashion, and the question as it stands doesn't really make sense. Please read http://tinyurl.com/so-list – Jon Skeet Apr 12 '13 at 07:42
  • Sorry that I gave less info, pls see my edited question – Indhi Apr 12 '13 at 07:45
  • @Alexei I have edited my question, kindly check – Indhi Apr 12 '13 at 07:52

5 Answers5

38

try this,

 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
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • The above result worked for me only when I convert it to boolean . Thanks @Saptal Singh – Indhi Apr 12 '13 at 09:09
7

You can use .Any() or .Count(). Any() is performing better. (Check this SO question to see why)

.Any()

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()

.Count()

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()
Community
  • 1
  • 1
Oscar Foley
  • 6,817
  • 8
  • 57
  • 90
5

If I understand you correctly you want to get true if one of the results of the query matches all conditions. In that case try something like this:

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();
Maciej
  • 7,871
  • 1
  • 31
  • 36
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" };
Skinny Pipes
  • 1,025
  • 6
  • 14
  • 1
    There's no reason to use an anonymous type here (I doubt this will even compile) and calling `ToString` on it definitely won't do what's required. – Jon Skeet Apr 12 '13 at 07:29
0

If you really want to have a "true" or "false" String Response:

    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();
        }
    }

But I would suggest to make the property "result" a bool and remove the ToString(). Once you have a bool you can always do a ToString() on it