在VB.NET中创建对象在不同的​​SQL服务器上的行为有所不同

时间:2016-05-20 08:22:44

标签: vb.net visual-studio-2012 linq-to-sql

我有以下代码,它们在不同的服务器上表现不同。如果我写下这行代码,在下面的方法中:

Dim customerPositionsFromPaid = vwCustomerPositionInPaid.SelectAll().Where(conditions).Select(Function(o) New CustomerPositionFromPaidDto(o.FundingYearId.Value, o.DsoId.Value, o.CustomerId.Value, o.CustomerPosition.Value)).ToList()    

它在一个sql实例上返回结果,但不会在其他sql实例上返回结果。

但是,如果我用以下内容替换上面的行,它将在两个sql实例上返回结果。

Dim customerPositionsFromPaid = vwCustomerPositionInPaid.
        SelectAll().
        Where(conditions).
        Select(Function(o) New CustomerPositionFromPaidDto() With {.FundingYearId = o.FundingYearId.Value, .DsoId = o.DsoId.Value, .CustomerId = o.CustomerId.Value, .CustomerPosition = o.CustomerPosition.Value}).
        ToList()

可能是因为sql server实例有不同的设置,或者它与代码本身有什么关系?

- 功能

Private Shared Function GetCustomerPositionsFromPaid(ByVal customerID As Integer, ByVal fundingYearID As Integer) As IEnumerable(Of CustomerPositionFromPaidDto)
    Dim conditions = PredicateBuilder.True(Of vwCustomerPositionInPaid)()
    conditions = conditions.And(Function(o) o.CustomerId.Equals(customerID))
    conditions = conditions.And(Function(o) o.FundingYearId.Equals(fundingYearID))
    conditions = conditions.And(Function(o) o.DsoId.HasValue)

    'Dim customerPositionsFromPaid = vwCustomerPositionInPaid.SelectAll().Where(conditions).Select(Function(o) New CustomerPositionFromPaidDto(o.FundingYearId.Value, o.DsoId.Value, o.CustomerId.Value, o.CustomerPosition.Value)).ToList()
    'Dim customerPositionsFromPaid = vwCustomerPositionInPaid.SelectAll().Where(conditions).Select(Function(o) New With {.FundingYearId = o.FundingYearId.Value, .DsoId = o.DsoId.Value, .CustomerId = o.CustomerId.Value, .CustomerPosition = o.CustomerPosition.Value}).ToList().Select(Function(o) New CustomerPositionFromPaidDto(o.FundingYearId, o.DsoId, o.CustomerId, o.CustomerPosition)).ToList()
    Dim customerPositionsFromPaid = vwCustomerPositionInPaid.
        SelectAll().
        Where(conditions).
        Select(Function(o) New CustomerPositionFromPaidDto() With {.FundingYearId = o.FundingYearId.Value, .DsoId = o.DsoId.Value, .CustomerId = o.CustomerId.Value, .CustomerPosition = o.CustomerPosition.Value}).
        ToList()
    Return customerPositionsFromPaid
End Function

- 全选

 Public Shared Function [SelectAll](ByVal conditions As Expression(Of Func(Of T, Boolean))) As IEnumerable(Of T)
    Return [SelectAll]().Where(conditions)
End Function

Public Shared Function [SelectAll]() As IQueryable(Of T)
    Return Table
End Function

Private Shared ReadOnly Property Table() As Table(Of T)
    Get
        Return Context.GetTable(Of T)()
    End Get
End Property

1 个答案:

答案 0 :(得分:-1)

我设法通过编写以下代码来解决上述问题。看起来因为customerid和fundingyearid是可以为空的对象,我不得不使用.Value属性,但仍不确定为什么以前的代码可以在一台服务器上运行而不在另一台服务器上运行。

Private Shared Function GetCustomerPositionsFromPaid(ByVal customerID As Integer, ByVal fundingYearID As Integer) As IEnumerable(Of CustomerPositionFromPaidDto)
Dim conditions = PredicateBuilder.True(Of vwCustomerPositionInPaid)()
conditions = conditions.And(Function(o) o.CustomerId.Equals(customerID.Value))
conditions = conditions.And(Function(o) o.FundingYearId.Equals(fundingYearID.Value))
conditions = conditions.And(Function(o) o.DsoId.HasValue)


Dim customerPositionsFromPaid = vwCustomerPositionInPaid.
    SelectAll().
    Where(conditions).
    Select(Function(o) New CustomerPositionFromPaidDto() With {.FundingYearId = o.FundingYearId.Value, .DsoId = o.DsoId.Value, .CustomerId = o.CustomerId.Value, .CustomerPosition = o.CustomerPosition.Value}).
    ToList()
Return customerPositionsFromPaid

结束功能