Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回布尔值的编译LINQ查询?_C#_Linq - Fatal编程技术网

C# 返回布尔值的编译LINQ查询?

C# 返回布尔值的编译LINQ查询?,c#,linq,C#,Linq,如何编写LINQ查询以返回Bool值 我的代码到目前为止 public class AddNewRow { public static Func<DatabaseDataContext, DateTime, int, Staff_Time_TBL> GetNewRowMissingData = CompiledQuery.Compile((DatabaseDataContext db, Date

如何编写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));
    }
公共类AddNewRow
{
公共静态函数
GetNewRowMissingData=
CompiledQuery.Compile((DatabaseDataContext数据库,日期时间dDate,int staffNo)=>
db.Staff\u Time\u TBLs.Any(a=>a.Date\u Data==dDate&&a.Staff\u No==staffNo));
}
也试过这个,

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());
    }
公共类AddNewRow
{
公共静态函数
GetNewRowMissingData=
CompiledQuery.Compile((DatabaseDataContext数据库,日期时间dDate,int staffNo)=>
db.Staff\u Time\u TBLs.Where(a=>a.Date\u Data==dDate&&a.Staff\u No==staffNo.Any());
}
因此,如果两个条件都满足,则返回true

我尝试过的任何其他代码都会把文章弄得乱七八糟

研究链接


另外,我还有我引用的那本书。

问题在于函数定义:

public static Func<DatabaseDataContext, DateTime, int, Staff_Time_TBL>
然后在您的查询中,我将使用
Any
,它将谓词作为更惯用的语句来给出最终结果:

public class AddNewRow
{
    public static Func<DatabaseDataContext, DateTime, int, bool>
        GetNewRowMissingData =
                 CompiledQuery.Compile((DatabaseDataContext db, DateTime dDate, int staffNo) =>
                 db.Staff_Time_TBLs.Any(a => a.Date_Data == dDate && a.Staff_No == staffNo));
}
公共类AddNewRow
{
公共静态函数
GetNewRowMissingData=
CompiledQuery.Compile((DatabaseDataContext数据库,日期时间dDate,int staffNo)=>
db.Staff\u Time\u TBLs.Any(a=>a.Date\u Data==dDate&&a.Staff\u No==staffNo));
}

这将返回您想要的答案。

您在这里真正想问什么?第一个LINQ查询是“更好的”/更惯用的@ChrisF,以使编译后的查询返回bool值?因此,我必须检查所有这些,以表明我在最后缺少了
bool
。谢谢你的回答,这就是我所追求的一切。投票支持这项努力,我将标出答案。
public class AddNewRow
{
    public static Func<DatabaseDataContext, DateTime, int, bool>
        GetNewRowMissingData =
                 CompiledQuery.Compile((DatabaseDataContext db, DateTime dDate, int staffNo) =>
                 db.Staff_Time_TBLs.Any(a => a.Date_Data == dDate && a.Staff_No == staffNo));
}