When I started using Entity Framework, I used Query Syntax as I was more used to using SQL than Method. Now I understand how to use Method, so I'm converting some of my older LINQ Query's to Method as I find that Method makes more sense from a programming perspective.
My question is, why does the compiler want me to use CBool() around some where items, but not others? It doesn't do this in C#, only VB.
Example;
This
TotalYearWages = If((From t In DB.interview_earnings
Where t.IID = Input.ID AndAlso t.EarningsYear = Input.EarningsYear
Select t.Amount).Sum, 0)
Becomes this
TotalYearWages = If(DB.interview_earnings.
Where(Function(t) t.IID = Input.ID AndAlso CBool(t.EarningsYear = Input.EarningsYear)).
Select(Function(t) t.Amount).Sum(), 0)
But in c# this works fine
TotalYearWages == DB.interview_earnings
.Where(t => t.IID == Input.ID && t.EarningsYear == Input.EarningsYear)
.Select(t => t.Amount).Sum() ?? 0
My question;
Why do I have to use CBool around some parts of the where clause, but not around others? What determines the need?
Edit;
The error I get without the CBool is;
Severity Code Description Project File Line Suppression State
Error BC30512 Option Strict On disallows implicit conversions from 'Boolean?' to 'Boolean'. WOTC-FE d:\Programming\WOTC-Projects\WOTC-FE\Class\Billing.vb 129 Active
The Class Constructors are;
Imports System
Imports System.Collections.Generic
Partial Public Class interview_earnings
Public Property ID As Integer
Public Property IID As Integer
Public Property CLIENTCODE As String
Public Property ClientLocation As String
Public Property EarningsDate As Nullable(Of Date)
Public Property UserName As String
Public Property Amount As Nullable(Of Decimal)
Public Property Hours As Nullable(Of Decimal)
Public Property InvDate As Nullable(Of Date)
Public Property InvNumber As Nullable(Of Short)
Public Property Notes As String
Public Property EarningsYear As Nullable(Of Short)
Public Property TCPercent As Nullable(Of Byte)
Public Property WTWBill As Nullable(Of Boolean)
Public Property WTW2yrBill As Nullable(Of Boolean)
Public Property Refund As Nullable(Of Boolean)
Public Overridable Property interview_main As interview_main
End Class
And
Input is
Public Class WageInfo
Public Property ID As Integer
Public Property NewYTDWage As Decimal?
Public Property Limit As Integer?
Public Property WOTC As Boolean
Public Property EarningsRemaining As Decimal?
Public Property EarningsYear As Short
Public Property ThirdYearCalculation As Boolean
Public Property WTW2Base As Decimal?
Public Property WageWTW2Yr As Decimal?
Public Property WageWOTC As Decimal?
Public Property StartDate As Date?
Public Property EarningsDate As Date
End Class
Query syntax and method syntax are semantically identical, but many people find query syntax simpler and easier to read. Some queries must be expressed as method calls. For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition.
The LINQ join methods are supported in LINQ to Entities, with the exception of those that accept an IEqualityComparer because the comparer cannot be translated to the data source. For more information, see Standard Query Operators in LINQ to Entities Queries.
LINQ query syntax is consist of a set of query keywords defined into the . NET Framework version 3.5 or Higher. This allows the programmer or developers to write the commands similar to SQL style in the code(C# or VB.NET) without using quotes. It is also know as the Query Expression Syntax.
When you want the underlying value of a nullable variable, use the Value method. This applies to both vb.net and c#.
TotalYearWages = If(DB.interview_earnings.
Where(Function(t) t.IID = Input.ID AndAlso t.EarningsYear.Value = Input.EarningsYear).
Select(Function(t) t.Amount).Sum(), 0)
I suggest you make these two fields the same type in your database, if that is logical.
And c# and vb.net compilers are different. Option Strict On
does not make vb.net behave exactly the same as c#.
Edit:
This solution will handle NULLs in interview_earnings.EarningsYear
TotalYearWages = If(DB.interview_earnings.
Where(Function(t) t.IID = Input.ID AndAlso t.EarningsYear.Equals(Input.EarningsYear)).
Select(Function(t) t.Amount).Sum(), 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With