سؤال

I have an IList--->

IList<Test> list;   

the list contains objects of the class Test, given below:

list.Add(new Test{ id = 1, name = "abc" })
list.Add(new Test{ id = 2, name = "xyz" })
list.Add(new Test{ id = 3, name = "nmo" })

where class Test is --->

Class Test
{
    int id;
    string name;
}

Now I want to select all name fields (of all elements of list)--->

IList<string> nameList = ???  

I got stuck in this (lack of knowledge about LINQ c#)

هل كانت مفيدة؟

المحلول 2

IList<string> nameList = YourCollection.Select(item=> item.name).ToList();

نصائح أخرى

You can use LINQ Select:

 nameList = list.Select(x => x.name).ToList();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top