在System.Linq命名空间,我们现在可以扩展我们IEnumerable的有Any()和Count() 扩展方法.
最近我被告知如果我想检查一个集合中是否包含一个或多个项目,我应该使用.Any()扩展方法而不是.Count() > 0扩展方法,因为.Count()扩展方法必须迭代所有项目.
其次,一些集合具有属性(未扩展方法),其是Count或Length.使用它们会更好吗,而不是.Any()或.Count()?
是啊/是?
如何编写LINQ查询以返回Bool值?
我的代码到目前为止,
public class AddNewRow
{
public static Func<DatabaseDataContext, DateTime, int, Staff_Time_TBL>
GetNewRowMissingData =
CompiledQuery.Compile((DatabaseDataContext db, DateTime dDate, int staffNo) =>
db.Staff_Time_TBLs.Any(a => a.Date_Data == dDate && a.Staff_No == staffNo));
}
Run Code Online (Sandbox Code Playgroud)
并试过这个,
public class AddNewRow
{
public static Func<DatabaseDataContext, DateTime, int, Staff_Time_TBL>
GetNewRowMissingData =
CompiledQuery.Compile((DatabaseDataContext db, DateTime dDate, int staffNo) =>
db.Staff_Time_TBLs.Where(a => a.Date_Data == dDate && a.Staff_No == staffNo).Any());
}
Run Code Online (Sandbox Code Playgroud)
因此,如果满足两个条件,则返回true.
我试过的任何其他代码都会让帖子变得杂乱无章.
研究链接,
另外,我有一本Pro C#5.0和.NET 4.5 Framework(.NET的专家语音)这本书.