简体   繁体   English

遍历列表对象以找到具有匹配属性值的 object

[英]Iterate through list objects to find object with matching property value

I have a list of objects (users) of a class (User).我有一个 class(用户)的对象(用户)列表。 Now I try to retrieve the (unique) object that has a certain property value (for example string username = name).现在我尝试检索具有特定属性值(例如字符串用户名 = 名称)的(唯一)object。 So what needs to be returned is the object, based on the value of its property.因此,根据其属性值,需要返回的是 object。

There are several similar questions asked here, but none seem to return the object as a whole.这里有几个类似的问题,但似乎没有一个整体返回 object 。

I've tried several methods including this one:我尝试了几种方法,包括这个:

public User GetUser(string name)
{
    User currentUser;

    for(int i = 0; i < this.users.Count; i++)
    {
        if (users[i].GetName().Equals(name))
        {
            users[i] = currentUser;
        }
    };

    return currentUser;
}

A problem here is that currentUser is not accepted as a variable.这里的一个问题是 currentUser 不被接受为变量。 What would be a better way to go about this?关于这个,go 有什么更好的方法吗?

EDIT: As stated in the answers the quickest solution for this issue is using LINQ. Also it was pointed out that there is a way to improve my code in order to reach the same result without using LINQ, as shown below:编辑:如答案中所述,此问题的最快解决方案是使用 LINQ。还指出,有一种方法可以改进我的代码,以便在不使用 LINQ 的情况下达到相同的结果,如下所示:

public User GetUser(string name)
        {           
            User currentUser = null;

            for (int i = 0; i < this.users.Count; i++)
            {
                if (users[i].GetName().Equals(name))
                {
                    currentUser = users[i];
                }
            };

            return currentUser;
        }

In case there are several users for which GetName equals to name, this method will return the last such user.如果有多个用户的 GetName 等于名称,此方法将返回最后一个这样的用户。

//     is optional, if you want ignore case
// users[i].GetName().Equals(name, StringComparison.InvariantCultureIgnoreCase)
public User? GetUser(string name)
{
    for(int i = 0; i < this.users.Count; i++)
        if (users[i].GetName().Equals(name))
            return users[i]; // early-return pattern
    return null; // returns null if no such user found
}

Dont forget to check for null in client code:不要忘记在客户端代码中检查 null:

var user = GetUser("SomeUserName");
if(user is null) {
    // do some
}

And if you like to use linq:如果你喜欢使用 linq:

public User? GetUser(string name) => users.FirstOrDefault(u => u.GetName().Equals(name));

A quick way to get what you want is to use LinQ获得所需内容的快速方法是使用 LinQ

var user = users.Where(user => user.GetName() == name).First();

or shorter或更短

var user = users.First(user => user.GetName() == name);

You can use LINQ :您可以使用LINQ

public User GetUser(string name)
{
    return users.FirstOrDefault(x => x.GetName().Equals(name));
}

Note : if such a name does not exist, the default will be returned, the default for this case is null .注意:如果不存在这样的名称,将返回默认值,这种情况下的默认值是null

It seems you are trying to do this看来你正在尝试这样做

var user = users.FirstOrDefault(u => u.GetName().Equals(name));

use the getter for username in the User object in place of GetName在用户 object 中使用用户名的 getter 代替 GetName

public User GetUser(string name)
{
   return users.FirstOrDefault(u => u.GetName().Equals(name));
}


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

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