C# 带有布尔检查的LINQ查询
我需要一个方法,它应该从stores表返回一个stores列表,其中customerid=id,Isactive=“true” 我能够像这样获得customerid匹配,我如何也包括布尔检查。。。需要查询语法方面的帮助..“AND”运算符C# 带有布尔检查的LINQ查询,c#,linq,C#,Linq,我需要一个方法,它应该从stores表返回一个stores列表,其中customerid=id,Isactive=“true” 我能够像这样获得customerid匹配,我如何也包括布尔检查。。。需要查询语法方面的帮助..“AND”运算符 private IList GetStores(int-id) { var stlist=db.Stores.Where(m=>m.CustomerId==id).ToList(); 返回(stlist); } 假设Isactive是db.Stores中记录的
private IList GetStores(int-id)
{
var stlist=db.Stores.Where(m=>m.CustomerId==id).ToList();
返回(stlist);
}
假设Isactive
是db.Stores
中记录的属性,就像CustomerId
一样
您只需在Where
扩展方法中添加附加检查:
假设
Isactive
是类型bool
private IList<Store> GetStores(int id)
{
var stlist = db.Stores.Where(m => m.CustomerId == id && m.Isactive).ToList();
return (stlist);
}
在C#
和许多其他语言中,&&
是布尔and运算符。私有IList GetStores(int-id)
private IList<Store> GetStores(int id)
{
var stlist = db.Stores.Where(m=>m.CustomerId == id && m.IsActive == true).ToList();
return stlist;
}
{
var stlist=db.Stores.Where(m=>m.CustomerId==id&&m.IsActive==true).ToList();
返回stlist;
}
或&&m.Isactive
:)@PhilVallone我想他提到了一个字符串Property@Sajeetharan如果是这样的话,那么上面的代码将失败,因为true
,因为你写的不是stringHaha,我看到你假设了同样的情况,并更改了你的答案:)的确如此。重读OP的问题后,我意识到它并不像看上去那么明显;-)编辑,因为不清楚所讨论的Isactive
属性是bool还是string谢谢您的回答!
private IList<Store> GetStores(int id)
{
var stlist = db.Stores.Where(m => m.CustomerId == id && m.Isactive).ToList();
return (stlist);
}
private IList<Store> GetStores(int id)
{
var stlist = db.Stores.Where(m => m.CustomerId == id && m.Isactive == "true").ToList();
return (stlist);
}
private IList<Store> GetStores(int id)
{
var stlist = db.Stores.Where(m=>m.CustomerId == id && m.IsActive == true).ToList();
return stlist;
}