-3

How to write a LINQ query to return Bool value?

My code thus far,

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));
    }

and tried this as well,

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());
    }

So if both criteria are met then return true.

Any of the other code I have tried will just clutter the post.

Research links,

Plus I have the book Pro C# 5.0 and the .NET 4.5 Framework (Expert's Voice in .NET) which I am referencing from.

Community
  • 1
  • 1
KyloRen
  • 2,691
  • 5
  • 29
  • 59

1 Answers1

6

The problem is in your function definition:

public static Func<DatabaseDataContext, DateTime, int, Staff_Time_TBL>

According to the MSDN page on compiled queries this would take a DateTime, and int as input parameters and return a Staff_Time_TBL as the result.

The last type is the return type so you need to have a bool at the end:

public static Func<DatabaseDataContext, DateTime, int, bool>

Then in your query I'd use the Any that takes a predicate as it's more idiomatic to give the final result:

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));
}

This should return you the answer you desire.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • So I had to go through all of that to say that I was missing `bool` at the end. Thanks a heap for the answer, that is all was after. Up vote fro the effort and I will mark the answer solved. – KyloRen Jun 18 '16 at 14:15