如何从列表(T)中检索相关项目 [英] How do I retrive related items from list(of T)

查看:63
本文介绍了如何从列表(T)中检索相关项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前我学会了如何使用词典列表,这样我就可以创建一个项目了。后来,需求发生了变化,我不得不看看List(T)。现在我有一个Contact类,其中包含:firstName,lastName,phoneNumber和birthday。



我可以将这些字段添加到列表中,但我不知道如何在需要时获取所有相关信息。我需要的是,一旦列表中有多个记录,我该如何检索所有相关字段。



那么,我该怎么做才能找到所有这些属于一起的信息?我怎么知道这个名字与这个姓氏有关?



我希望这是有道理的。如果没有,请告诉我,我会再试一次。



感谢您的帮助。



我尝试了什么:



这是我目前使用的代码,只是为了加载,所以我可以看到它是有效的。



将此联系人视为新列表(联系人)()



'添加联系信息到列表。

thisContact.Add(New Contact()With {

.FirstName =Joe,

.LastName =Langley ,

。电话号码=2175443,

。生日= CDate(05/16/1954)})



For thisContact中的每个细节

MsgBox(名字:& detail.FirstName _

& Environment.NewLine&姓氏:& detail.LastName _

& Environment.NewLine _

& Environment.NewLine&电话号码:& detail.PhoneNumber _

&环境。新浪潮& birthDaty:& detail.Birthday)

下一步

解决方案

创建类时,为其赋予了属性:FirstName和LastName。这意味着一个类的每个实例都有它自己的LastName和FirstName集合,它们不与任何其他实例共享。

如果你愿意,可以想一下汽车。每辆车都有自己的钥匙,而且还有自己的手套箱。

拿五辆车,每辆车只用自己的钥匙打开 - 每辆车内都是一个单独的手套箱。如果你把一件物品 - 比如你的手机 - 放在绿色汽车的手套箱里,那么你不会期望在红色或蓝色汽车中找到它,不是吗?

所以如果你创建了两个Contact类的实例(通过使用New两次)每个Contact实例都有独立的FirstName和LastName:

  Dim  firstPerson  As   New  Contact()
firstPerson.FirstName = joe
firstPerson.LastName = smith
Dim secondPerson As 联系人()
secondPerson.FirstName = mike
secondPerson.LastName = hunt
Console.WriteLine( {0}:{1} ,firstPerson.FirstName,firstPerson.LastName)
Console.WriteLine( {0}: {1},secondPerson.FirstName,secondPerson.LastName)





有没有更好的方法跟上第一人和第二人并且继续?



当然有!只需使用List(Of Contact):

  Dim 联系人作为 列表( 联系人)()
Dim newContact 作为 联系人()
newContact.FirstName = joe
newContact.LastName = smith
contacts.Add(newContact)
newContact = 联系人()
newContact.FirstName = mike
newContact.LastName = hunt
contacts.Add(newContact)
对于 每个 c As 联系联系人
Console.WriteLine( {0}:{1},c.FirstName,c.LastName)
下一页





忘记将第二个联系人添加到列表中! :o [/编辑]


Hi, a few days ago I learned how to use a Dictionary list so I could create a project. Later, requirements changed and I had to look at List(of T). Right now I have a Contact class that contains: firstName, lastName, phoneNumber, and birthday.

I'm able to add these fields to the list, but I don't know how to get all the related information back when I need it. What I need is, once I have multiple records in the list, how do I retrieve all the related fields.

So, what do I do to find all this information that belong together? How do I know that this first name goes with this last name?

I hope this makes sense. If it doesn't, just let me know and I will try again.

Thanks for any help.

What I have tried:

Here's my current code that I've used just to load things up so I can see how it works.

Dim thisContact As New List(Of Contact)()

' Add Contact info to the list.
thisContact.Add(New Contact() With {
.FirstName = "Joe",
.LastName = "Langley",
.PhoneNumber = "2175443",
.Birthday = CDate("05/16/1954")})

For Each detail In thisContact
MsgBox("First Name: " & detail.FirstName _
& Environment.NewLine & "Last name: " & detail.LastName _
& Environment.NewLine _
& Environment.NewLine & "Phone Number: " & detail.PhoneNumber _
& Environment.NewLine & "birthDaty: " & detail.Birthday)
Next

解决方案

When you created the class, you gave it properties: FirstName and LastName. Which means that each instance of a class has it's own set of LastName and FirstName, which aren;t share with any other instance.
If you like, think of a car. Each car has it's own key, and it's own glove box.
Take five cars, and each will open with it's own key only - and inside each car is a separate glove box. If you place an item - your mobile phone, say - in the glove box of the Green car, then you wouldn't expect to find it in the Red or Blue cars, would you?
So if you create two instance of your Contact class (by using New twice) each Contact instance has independant FirstName and LastName:

Dim firstPerson As New Contact()
firstPerson.FirstName = "joe"
firstPerson.LastName = "smith"
Dim secondPerson As New Contact()
secondPerson.FirstName = "mike"
secondPerson.LastName = "hunt"
Console.WriteLine("{0}:{1}", firstPerson.FirstName, firstPerson.LastName)
Console.WriteLine("{0}:{1}", secondPerson.FirstName, secondPerson.LastName)



" Is there a better way to keep up with firstperson and secondperson and on and on?"

Of course there is! Just use a List (Of Contact):

Dim contacts As New List(Of Contact)()
Dim newContact As New Contact()
newContact.FirstName = "joe"
newContact.LastName = "smith"
contacts.Add(newContact)
newContact = New Contact()
newContact.FirstName = "mike"
newContact.LastName = "hunt"
contacts.Add(newContact)
For Each c As Contact In contacts
	Console.WriteLine("{0}:{1}", c.FirstName, c.LastName)
Next



[edit]Forgot to add the second contact to the List! :O[/edit]


这篇关于如何从列表(T)中检索相关项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆