LINQ查询返回TRUE或FALSE [英] Linq query return true or false
本文介绍了LINQ查询返回TRUE或FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询,它应该返回TRUE或FALSE。
VAR的查询=从C在db.Emp
从db.EmpDetails
D,其中c。 ID == d.ID和放大器;&安培; c.FirstName ==A和放大器;&安培; c.LastName ==D
//它应该在这个上面的说法符合所有这些条件
$ B返回TRUE $ b
和我想这个查询结果附加到一个属性(字符串数据类型)
this.result = Conert .ToBoolean(查询);
如何在LINQ实现这一目标?
编辑:
EmpMapper类
公共类EmpMapper
{
EmpEntities分贝;
// ID列已存在于数据库
私人诠释身份证;
//我创建这个属性将其从UI侧增加,这取决于在查询中的某些条件。这就是为什么我创建了一个单独的类来映射从DB
布尔结果现有的ID;
公共EmpMapper(INT ID,布尔结果)
{
this.db =新EmpEntites();
this.ID = ID;
VAR的查询=从C与D在db.EmpDetails
db.Emp
,其中c.ID == d.ID&放大器;&安培; c.FirstName ==A和放大器;&安培; c.LastName ==D
//它应该在这个上面的说法符合所有这些条件
this.result = Convert.ToBoolean(查询)返回TRUE;
}
公众诠释ID
{
{返回this.ID;}
集合{this.ID =值;}
}
酒店的公共布尔结果
{
{返回this.result;}
集合{this.result =值;}
}
}
MainViewModel类
名单,LT ; EmpMapper> empMapCol =新的List< EmpMapper>();
私人无效PAGE_LOADED(对象发件人,RoutedEventArgs E)
{
VAR emp_query =从C在db.Emp
排序依据c.ID
选择;
名单,LT;&的Emp GT; empCol = emp_query.ToList();
的foreach(在empCol的Emp项)
{
this.empMapCol.Add(新EmpMapper(item.ID,item.result));
}
datagrid1.ItemsSource = empMapCol;
}
}
解决方案
尝试对此,
VAR的查询=(从C与D在db.EmpDetails
db.Emp
其中,c.ID == d.ID&放大器;&安培; c.FirstName ==A和放大器;&安培; c.LastName ==D
选择C
)。任何();
this.result =查询; //无需转换为boolean其已经布尔值
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;
}
}
解决方案
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
这篇关于LINQ查询返回TRUE或FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文