FirstOrDefault()== null? false:是的;

时间:2014-12-02 11:20:11

标签: c# linq

任何人都可以解释这行代码吗?

bool status = datacontext.tblTransactionDetails.Where(x => x.AdvertID == app.AdvertID && x.IsActive == true).FirstOrDefault() == null ? false : true;

1 个答案:

答案 0 :(得分:6)

这意味着从广告系列== app.AdvertID&& IsActive == true。如果它的null返回false,否则返回true。

? :语法称为三元运算符,用作if / else。

的简写

相反,你可以使用

.Any(x => x.AdvertID == app.AdvertID && x.IsActive == true) 

如果符合条件,则返回true,否则返回false。

完整的代码行是:

bool status = datacontext.tblTransactionDetails.Any(x => x.AdvertID == app.AdvertID && x.IsActive == true);