操作结构数组列表

时间:2017-12-10 05:27:24

标签: .net vb.net linq

我有一个结构例如:

Structure User
 Dim Name as string
 Dim ID as Short
 Dim Email as string
End Structure

现在主要的数组声明是:Dim Client作为User的新列表

这个新的数组列表包含所有记录。现在如何在不使用循环的情况下执行以下操作,例如:
1)我需要将所有不同的名称填充到组合框中。如果它是字符串数组我可以完成

Combobox1.addrange(tmpstring.distinct.toarray)

但是因为这是一种类型的结构(例如用户)该怎么办?

2)如何使用linq仅将一组字段数据填充到数组中。对于例如如果我在客户端b中执行Dim A = b,其中b.id> 0'然后返回所有数组项。如何获得唯一ID?

谢谢。

2 个答案:

答案 0 :(得分:1)

1)我不熟悉VB,所以这是C#中的解决方案:

var names = Client
  .Select(user => user.Name)
  .Distinct();

Telerik Code Converter将其转换为:

Private names = Client.[Select](Function(user) user.Name).Distinct()

2)如果我理解你,你想要一个包含id的数组吗?您应该将查询扩展到:

Dim A = from b in Client 
        where b.id>0 
        select b.id

答案 1 :(得分:1)

List(Of Class)(引用类型)比Structure(值类型)更灵活。

使用列表(结构):

Dim Clients As New List(Of User)

Structure User
    Dim Name As String
    Dim ID As Short
    Dim Email As String
End Structure

'Add some elements to it
Clients.Add(New User With {.Name = "User1", .ID = 1, .Email = "Email1"})
Clients.Add(New User With {.Name = "User2", .ID = 2, .Email = "Email2"})
Clients.Add(New User With {.Name = "User3", .ID = 3, .Email = "Email3"})

要填写ComboBox列表,您可以:

'Fill it with the whole list
ComboBox1.Items.AddRange(Clients.Select(Function(c) c.Name).ToArray)

'Or filter it using a range of values
Dim SublistClient As New List(Of User)
SublistClient.AddRange(Clients.Select(Function(c) c).Where(Function(c) (c.ID > 1 And c.ID < 3)))

ComboBox1.Items.AddRange(SublistClient.Select(Function(c) c.Name).ToArray)

当用户从列表中选择项目时,获取项目值:

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    Dim SelectedUser As User = Clients(ComboBox1.SelectedIndex)
    Console.WriteLine("Name: {0}, ID: {1}, Email: {2}", SelectedUser.Name, SelectedUser.ID, SelectedUser.Email)
End Sub

使用List(Of Class)

Dim Clients As New List(Of User)

Class User
   Public Property Name As String
   Public Property ID As Short
   Public Property Email As String
End Class

  'You can add items to the list in the same way, nothing will change here
  Clients.Add(New User With {.Name = "User1", .ID = 1, .Email = "Email1"})
  Clients.Add(New User With {.Name = "User2", .ID = 2, .Email = "Email2"})
  Clients.Add(New User With {.Name = "User3", .ID = 3, .Email = "Email3"})

现在,您可以使用其.DataSource属性

填充ComboBox
  'Add the whole List as the ComboBox `.DataSource`
  ComboBox1.DataSource = Clients

  'Or filter it using a range of values
  ComboBox1.DataSource = (Clients.Select(Function(c) c).Where(Function(c) (c.ID > 1 And c.ID < 3))).ToList()

  'Or from a specific value up or down
  ComboBox1.DataSource = (Clients.Select(Function(c) c).Where(Function(c) c.ID > 1)).ToList()

  'This gives you extended options when you have to present your data
  'or evaluate the result of a choice
  ComboBox1.DisplayMember = "Name"
  ComboBox1.ValueMember = "ID"

'The user choice is evaluated more or less in the same manner
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
   Dim SelectedUser As User = CType(ComboBox1.SelectedItem, User)
   Console.WriteLine("Name: {0}, ID: {1}, Email: {2}", SelectedUser.Name, SelectedUser.ID, SelectedUser.Email)
End Sub