LINQ查询到LINQ方法;为什么我需要CBool​​?

时间:2018-05-24 13:51:43

标签: vb.net linq lambda entity-framework-6

当我开始使用Entity Framework时,我使用了查询语法,因为我比使用SQL更习惯使用SQL。现在我了解了如何使用Method,所以我将一些旧的LINQ Query转换为Method,因为我发现Method从编程角度来看更有意义。

我的问题是,为什么编译器要我在某些项目周围使用CBool​​(),而不是其他项目?它不是在C#中执行此操作,只是VB。

实施例

TotalYearWages = If((From t In DB.interview_earnings
    Where t.IID = Input.ID AndAlso  t.EarningsYear = Input.EarningsYear
    Select t.Amount).Sum, 0)

成为这个

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)

但是在c#中这很好用

TotalYearWages == DB.interview_earnings
    .Where(t => t.IID == Input.ID && t.EarningsYear == Input.EarningsYear)
    .Select(t => t.Amount).Sum() ?? 0

我的问题;

为什么我必须围绕where子句的某些部分使用CBool​​,而不是围绕其他部分使用?是什么决定了需求?

编辑;

没有CBool​​的错误是;

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

班级建设者是;

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

输入

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

1 个答案:

答案 0 :(得分:1)

如果需要可以为null的变量的基础值,请使用Value方法。这适用于vb.net和c#。

gremlin> g.addV('Person').property('name', 'Jack').property('entityId', '100').as('jack').
......1>   addV('Person').property('name', 'John').property('entityId', '50').as('john').
......2>   addE('managerOf').from('jack').to('john').property('inEntityId', '50').property('outEntityId', '100').iterate()
gremlin> g.V().outE().inV().path().by('name').by(valueMap())
==>[Jack,[inEntityId:50,outEntityId:100],John]

我建议你在数据库中使这两个字段的类型相同,如果这是合乎逻辑的。

c#和vb.net编译器不同。 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) 不会使vb.net的行为与c#完全相同。

编辑:

此解决方案将在interview_earnings.EarningsYear

中处理NULL
Option Strict On