Is there any way to get the values of items in the IList? It goes like this:
Assume we have an IList list
and a method private IList getValues(int ID)
then we initialize IList list
like:
IList list = getValues(SelectedID)
After the processing, IList list
now contains 2 elements, each with it's own collection:
[0]
{
GroupName = "test01"
GroupID = 1
}
[1]
{
GroupName = "test02"
GroupID = 2
}
How will I get the GroupName of the first element?
I can't just call it like list[0].GroupName
, can I?
If you know the type of the object, you can use this to get what you want.
list.Cast<Group>().ElementAt(0).GroupName
Group as in your object name.
Try the following, you need to create a list with your custom object. I created a dummy class here named CustomClass, and creating a list with the known type.
public class CustomClass
{
public string GroupName { get; set; }
public int GroupID { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
List<CustomClass> list = new List<CustomClass>();
foreach (var x in list) {
Console.WriteLine(x.GroupName);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With