循环通过实体框架复杂类型

时间:2018-04-26 18:05:45

标签: vb.net visual-studio entity-framework

尝试使用实体框架循环存储过程的结果。我需要将社区列表与文本框中的值进行比较,以便用户不会使用重复标记在数据库中输入重复的社区。我可以检索我的社区列表,但是我在循环查看列表时遇到了困难。

    Dim duplicate = False
    Dim list = Accommodations.GetCommunities
    For Each n As String In list.CommunityName
        If n = txtCommunities.Text Then
            duplicate = True
        End If
    Next n

调试时我可以将鼠标悬停在Accommodations.GetCommunities上,并查看我需要在“CommunityName”字段下循环的所有值,但是当我逐步执行循环时,n的值显示为单个字符。有没有办法将此结果集转换为列表,以便我可以遍历“CommunityName”

下的每个值

我也尝试了下面的代码,并且由于某种原因它将com设置为等于复杂类型的名称,但它正确地循环遍历列表中正确数量的项目。如何提取该字段以将其与文本框进行比较?

    Dim duplicate = False
    Dim com As String = String.Empty
    Dim list = Accommodations.GetCommunities
    For i As Integer = 0 To list.count - 1
        com = list(i).ToString
        If com = txtCommunities.Text Then
            duplicate = True
        End If
    Next
    Return duplicate

最终编辑:

这是我之后使用的功能。使用String.Compare()方法比较字符串以忽略大小写。

 Private Function checkDuplicates()
    Dim duplicate = False

    Dim list = Accommodations.GetCommunities 'Put items in a list
    For i As Integer = 0 To list.count - 1 'loop through the list
        If String.Compare(list(i).CommunityName, txtCommunities.Text, True) = 0 Then 'Compare the two strings, comparrison is not case sensitive
            duplicate = True 'set dup flag to true
            Exit For 'exit loop
        End If
    Next
    Return duplicate

End Function

3 个答案:

答案 0 :(得分:1)

我认为它看起来应该更像

Dim duplicate = False
Dim list = Accommodations.GetCommunities
For Each community As [something] In list
    If community.CommunityName = txtCommunities.Text Then
        duplicate = True
    End If
Next n

Dim duplicate = False
Dim list = Accommodations.GetCommunities
For i As Integer = 0 To list.count - 1
    If list(i).CommunityName = txtCommunities.Text Then
        duplicate = True
    End If
Next
Return duplicate

附注:尝试使用好的变量名而不是n。此外,当您找到重复项时,可以退出循环或立即返回True。

答案 1 :(得分:1)

在第一种情况下,您要将n与CommunityName中的每个字符进行比较。你应该做点什么:

For Each n As String In list
    If n.CommunityName = txtCommunities.Text Then

在第二种情况下,列表(i)是列表中的社区。因此list(i).ToString()显示变量的名称。

答案 2 :(得分:1)

样本1

Ther也是使用字典的选项,如果您将列表作为字典,如果您通过唯一键值添加或删除,则不需要循环

Public Class TestClass
    Property Name As String
End Class
Private Function TestFun() As Boolean
    'Sample List to convert use Accommodations.GetCommunities
    Dim List As New List(Of TestClass)
    List.Add(New TestClass With {.Name = "a"})
    List.Add(New TestClass With {.Name = "b"})
    'If at Begining all elements by key are unique u can convert to dictionary
'
'From This Point u can map your list in to dictionary Use Accommodations.GetCommunities instant List. and type of what use in this collection replece as TestClass
    Dim CheckInDictionary As Dictionary(Of String, TestClass) = List.ToDictionary(Function(p) p.Name, Function(p) p)
    ' After you can us if some key or Id exist
    Return CheckInDictionary.ContainsKey("a")
End Function 'Return True

样本2

Ther也是检查列表是否有重复的另一种可能性,但这更加复杂。

List has method包含检查所述列表中是否存在元素,因此在添加新元素之前,您可以检查列表是否包含该元素。比较使用方法相等,所以如果你在你的基类中覆盖它,你可以做相同的特殊规则。

Public Class TestClass
    Property Name As String
    Public Overrides Function Equals(obj As Object) As Boolean
        'IMPORTEND Input is as object if anny case will be somthing diffrent then this type it can meak exception
        If DirectCast(obj, TestClass).Name = Me.Name Then Return True
        Return MyBase.Equals(obj)
    End Function
End Class
     

之后你尝试添加新元素u dolike

    Dim NewEle = New TestClass With {.Name = "a"}
    If Not List.Contains(NewEle) Then
        List.Add(NewEle)
    End If