Kaishu February 2016

How to find item in ArrayList filled with list of userdefined class?

ArrayList contains list of items of type clsPMLookUpValue

class clsPMLookUpValue
    public string LongValue;
    public string ShortValue;
    public string value;

I want to get ShortValue from ArrayList item which has Value = 101202. In short want to get ShortValue by checking Value.
How to do that?
Can we do that by using lambda expression or by other way?


Mr Smart February 2016

var obj = list.FirstOrDefault(a=>a.Value == "101202");
      String shortValue= obj.ShortValue;

fubo February 2016

Assuming your ArrayList is declared that way

System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add(new clsPMLookUpValue() { value = "101202", ShortValue = "ShortValue", LongValue = "LongValue" });

this is the way to select the ShortValue

string result = al.Cast<clsPMLookUpValue>().Where(x => x.value == "101202").Select(x => x.ShortValue).First();

Majed February 2016

new ArrayList(listOld.Cast<clsPMLookUpValue>() .Where(i => i.value == "101202") .Select(i=>i.ShortValue).ToList());

Post Status

Asked in February 2016
Viewed 2,142 times
Voted 6
Answered 3 times


Leave an answer