简体   繁体   English

通过列表中某项的属性值从通用列表中检索对象

[英]Retrieving an object from a generic List by property value of an Item in the list

I have a custom class that inherits from List(of T). 我有一个自定义类,它继承自List(of T)。 How can I find an object from the List with a custom property? 如何从具有自定义属性的列表中找到对象?

Sample code: 样例代码:

Public class baseClass
    Property ID
End class

Public class Member
   Inherits BaseClass

   Property UserID As Integer
End Class

Public Class LCMSCollection(Of T As BaseClass)
    Inherits System.Collections.Generic.List(Of T)


End Class

What I want to be able to do is find a member by UserId. 我想要做的是通过UserId查找成员。 I would prefer something like this: 我希望这样的事情:

Dim userId = 3
Dim members = MemberService.GetMembers(<some parameter>)
Dim member = members.<some method name>(userId)

In other circumstances it would be: 在其他情况下,它将是:

Dim playerid = 6
Dim team = TeamService.GetTeam(<some parameter>)
Dim player  = teams.<some method name>(playerid)

I'm thinking about a delegate and changing the Property to a function, but I don't know how to proceed. 我正在考虑一个委托并将Property更改为一个函数,但是我不知道如何进行。

Currently I have this: 目前我有这个:

Dim playerid = 6
Dim team = TeamService.GetTeam(<some parameter>)
Dim player = TeamService.GetPlayer(team, playerid)

This is not satisfying for several reasons. 由于几个原因,这不能令人满意。 First of all getting an item from a collection should be done on the collection like getting an item from a list. 首先,应该从集合中获取项目,就像从列表中获取项目一样。 Futhermore in the current situation one needs to add custom methods for each an every instance type of the collection. 此外,在当前情况下,需要为集合的每种实例类型的每一种添加自定义方法。 To avoid that one could create a general method getItem in a base class ad have all repositories like in this example Teams and Members inherit from this base class. 为了避免这种情况,可以在基类广告中创建一种通用方法getItem ,该广告具有所有存储库,如本例中所示, TeamsMembers从该基类继承。 BUt this cannot account for the fact that every instance class of baseclass has a different property or method to use as a filter. 但是,这不能说明以下事实,即基类的每个实例类都有不同的属性或方法用作过滤器。 Doing it like this seems to me to introduce interdependencies and complexity which I was trying to avoid by using a generic collection. 在我看来,这样做是为了引入相互依赖性和复杂性,而我试图通过使用通用集合来避免这种相互依赖性。

Any suggestions? 有什么建议么? Am I trying to solve a non issue or approaching this from the wrong angle. 我是要解决一个非问题还是从错误的角度解决这个问题。

Btw I use both C# and VBnet so answers in either one language is OK 顺便说一句,我同时使用C#和VBnet,所以用任何一种语言回答都可以

Use LINQ . 使用LINQ

Dim teams As List(Of Team) = [your team list]
Dim id As Integer = 10
Dim match As Team = (From item As Team In teams Select item Where item.ID = id).FirstOrDefault()

So the idea is that you want to 'filter' your list by some condition? 因此,您的想法是要根据某种条件“过滤”列表? Try this (C#): 试试这个(C#):

Player match = PlayerList.Find(p => (p.ID == 5));

Will scan your collection and return the first match. 将扫描您的收藏并返回第一个匹配项。 Alternatively, if you want all matches, you can use FindAll, which returns an IEnumerable. 或者,如果要所有匹配项,则可以使用FindAll,它返回IEnumerable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM