C# Linq 查询返回 true 或 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15965423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq query return true or false
提问by Indhi
I have a query where it should return TRUE or FALSE.
我有一个查询,它应该返回 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
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 ?
如何在 LINQ 中实现这一点?
EDIT:
编辑:
EmpMapper class
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 class
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;
}
}
采纳答案by Satpal
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
回答by Skinny Pipes
var query = from c in db.Emp
from d in db.EmpDetails
select new { c.ID == y.ID &&
c.FirstName == "A" &&
c.LastName == "D" };
回答by Oscar Foley
You can use .Any() or .Count(). Any()is performing better. (Check this SO questionto see why)
您可以使用 .Any() 或 .Count()。Any()表现更好。(检查这个SO 问题以了解原因)
.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()
回答by Maciej
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:
如果我理解正确,您希望获得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();
回答by user2141886
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
但我建议将属性“result”设为 bool 并删除 ToString()。一旦你有一个 bool 你就可以在它上面做一个 ToString()