How to search for an object in List<object> having its field value

☆樱花仙子☆ 提交于 2021-02-09 11:52:11

问题


The question is how to get an object from List having its field value

I have got list called f_objects filled with objects.

private List<F_object> f_objects;

i've got also a string with some value :

string name = "something";

F_object got a method returning its field called name:

public string GetName()
{
    return this.name;
}

Is there a built in method for compare objects in list vs this field value ? Or should i make a loop and compare like this:

foreach(F_object ob in f_objects)
{
    if String.Equals(name, ob.GetName())
    F_object found = ob;
} 

回答1:


There are multiple ways of doing this in linq. First one is to use .Where. This would suite your needs if there can be more matches

IEnumerable<F_object> result = f_objects.Where(obj=>obj.GetName() == name);

Second option is if you are only concerned about the first hit, even if there could be more than one, in that case use .First

f_object result = f_objects.First(obj=>obj.GetName() == name);

or if zero hits is an accepted situation (non-exceptional)

f_object result = f_objects.FirstOrDefault(Obj=>obj.GetName() == name);

if you should only ever have one hit then use .Single

f_object result = f_objects.Single(obj=>obj.GetName() == name);

or if you can have only one or zero hits then use .SingleOrDefault

f_object result = f_objects.SingleOrDefault(obj=>obj.GetName() == name);



回答2:


Using LINQ:

List<F_object> found = f_objects.Where(obj => obj.GetName() == name).ToList();

That's a list of all the matching objects.

Edit: Rune FS' answer also has good advice for using .Single() or .First() instead of .Where() if you expect or only care about grabbing one distinct value.




回答3:


List<...> items = ... ;
string name = ...;
var found = items.Find( i => string.Equals(name, i.GetName()) );



回答4:


You could use LINQ.

var objectswith_something = f_objects.Where(fo => fo.GetName().Equals(name));

If you just want the first, use

f_objects.Where(fo => fo.GetName().Equals(name)).FirstOrDefault();`



回答5:


There are still people that cannot use LINQ in theirs projects, so here is version without it:

F_object found = f_objects.Find(delegate(F_object f)
{
    return f.GetName().Equals(name);
});



回答6:


the best way is to search using the Linq Expressions assuming you are using 3.5 or above.

For example you would write

IEnumerable<F_object> filteredListOff_objects f_objects.Where(f_object=>f_object.GetName() == "something");

To get an IEnumberable of the objects that have the name "something"




回答7:


Another version using the Query Syntax. I think this is more readable than the code with the lambda expressions. In the end, it will be the same.

string name = "something";
var foundQuery = from s in f_objects
                 where s.Name == name
                 select s;

I would declare a property Name instead of GetName(). Then you can iterate with foreach over the query:

foreach (string s in foundQuery)
   ...

or convert it to a List<> or an Array with ToList or ToArray.



来源:https://stackoverflow.com/questions/8249002/how-to-search-for-an-object-in-listobject-having-its-field-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!