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 &&
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
var query = from c in db.Emp
from d in db.EmpDetails
select new { c.ID == y.ID &&
c.FirstName == "A" &&
c.LastName == "D" };
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
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();
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()