【问题标题】:WHERE IN clause using VB.NET in Entity Framework在实体框架中使用 VB.NET 的 WHERE IN 子句
【发布时间】:2013-07-18 05:31:46
【问题描述】:

我有两个 CheckboxLists。根据检查的项目,检查的值被连接成一个逗号分隔的字符串并传递给我的实体框架方法。结果将是一个 List(Of Entity)。

我要转换

SELECT * 
FROM dbo.Findings /*Below criteria is added only if any of the values are checked*/
WHERE FindingCategoryId IN (<Checked Values>) 
  AND FindingSeverityId IN (<CheckBoxValues>)

我无法在 EF 中找到 IN for VB.Net 的等效项。

我查看了 C# 帖子 here 并想出了以下代码。我收到一个错误

无法创建“System.Object”类型的常量值。此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。

如何在 Vb.Net 中使用 IN 子句?任何帮助表示赞赏。

我的代码:

Public Function Findings(ByVal findingSeverity As String, ByVal findingCategory As String) As List(Of Finding)
    Dim aTypeLoanId As Integer = If(Not IsNothing(AuditTypeLoanId), AuditTypeLoanId, -1)
    Dim findingTargetId = CInt(FindingTarget.LoanHeader)
    Using e As New LQCEntities()
        Dim result = (From f In e.FindingEntities _
                From hmd In e.HeaderMetaDataEntities.Where(Function(h) h.MetaDataId = f.TargetId).DefaultIfEmpty() _
                From cl In e.CheckListEntities.Where(Function(c) c.CheckListId = f.TargetId).DefaultIfEmpty() _
                Where f.AuditTypeLoanId = aTypeLoanId _
                Select New Finding With _
                       {
                            .FindingId = f.FindingId, _
                            .FindingCategoryId = f.FindingCategoryId, _
                            .FindingSeverityId = f.FindingSeverityId, _
                            .EnteredBy = f.ADUser.DisplayName, _
                            .EnteredDate = f.EnteredDate _
                        })
        Dim fsArray() = Nothing
        Dim fcArray() = Nothing
        If (Not String.IsNullOrEmpty(findingSeverity)) Then
            Dim fs = findingSeverity.Split(",")
            For i As Integer = 0 To fs.Count - 1
                Dim j As Integer = 0
                If (Integer.TryParse(fs(i), j)) Then
                    ReDim Preserve fsArray(i)
                    fsArray(i) = j
                End If
            Next
            If (fsArray.Count > 0) Then
                result = result.Where(Function(i) fsArray.Contains(i.FindingSeverityId))
            End If
        End If
        If (Not String.IsNullOrEmpty(findingCategory)) Then
            Dim fc = findingCategory.Split(",")
            For i As Integer = 0 To fc.Count - 1
                Dim j As Integer = 0
                If (Integer.TryParse(fc(i), j)) Then
                    ReDim Preserve fcArray(i)
                    fcArray(i) = j
                End If
            Next
            If (fcArray.Count > 0) Then
                result = result.Where(Function(i) fcArray.Contains(i.FindingCategoryId))
            End If
        End If
        Return result.ToList()
    End Using
End Function

【问题讨论】:

    标签: vb.net entity-framework where-in


    【解决方案1】:

    我将 fsArray 和 fcArray 更改为 List(Of Integer) 并且它起作用了。 代码如下:

    Public Function Findings(ByVal findingSeverity As String, ByVal findingCategory As String) As List(Of Finding)
            Dim aTypeLoanId As Integer = If(Not IsNothing(AuditTypeLoanId), AuditTypeLoanId, -1)
            Dim findingTargetId = CInt(FindingTarget.LoanHeader)
            Using e As New LQCEntities()
                Dim result = (From f In e.FindingEntities _
                        From hmd In e.HeaderMetaDataEntities.Where(Function(h) h.MetaDataId = f.TargetId).DefaultIfEmpty() _
                        From cl In e.CheckListEntities.Where(Function(c) c.CheckListId = f.TargetId).DefaultIfEmpty() _
                        Where f.AuditTypeLoanId = aTypeLoanId _
                        Select New Finding With _
                               {
                                    .FindingId = f.FindingId, _
                                    .AuditTypeLoanId = f.AuditTypeLoanId, _
                                    .FindingCategoryId = f.FindingCategoryId, _
                                    .CategoryDescription = f.FindingCategory.CategoryDescription, _
                                    .FindingSeverityId = f.FindingSeverityId, _
                                    .SeverityDescription = f.FindingSeverity.SeverityDescription, _
                                    .TargetId = f.TargetId, _
                                    .UserResponse = f.UserResponse, _
                                    .Field = If(f.FindingTargetId = findingTargetId, hmd.ColumnDescription, cl.CheckListDesc), _
                                    .OldValue = f.OldValue, _
                                    .NewValue = f.NewValue, _
                                    .Comments = f.Comments, _
                                    .EnteredBy = f.ADUser.DisplayName, _
                                    .EnteredDate = f.EnteredDate _
                                })
    
    
                If (Not String.IsNullOrEmpty(findingSeverity)) Then
                    Dim fsList As New List(Of Integer)
                    Dim fs = findingSeverity.Split(",")
                    For i As Integer = 0 To fs.Count - 1
                        Dim j As Integer = 0
                        If (Integer.TryParse(fs(i), j)) Then
                            fsList.Add(j)
                        End If
                    Next
                    If (fsList.Count > 0) Then
                        result = result.Where(Function(i) fsList.Contains(i.FindingSeverityId))
                    End If
                End If
                If (Not String.IsNullOrEmpty(findingCategory)) Then
                    Dim fc = findingCategory.Split(",")
                    Dim fcList As New List(Of Integer)
                    For i As Integer = 0 To fc.Count - 1
                        Dim j As Integer = 0
                        If (Integer.TryParse(fc(i), j)) Then
                            fcList.Add(j)
                        End If
                    Next
                    If (fcList.Count > 0) Then
                        result = result.Where(Function(i) fcList.Contains(i.FindingCategoryId))
                    End If
                End If
                Return result.ToList()
            End Using
        End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2019-10-13
      • 2010-11-08
      • 2011-05-11
      • 1970-01-01
      相关资源
      最近更新 更多