I often find myself needing to compose a list of items from attributes of another list of items.
An approach I take is often similar to the following:
public class MyClass
{
public Guid Identifier { get; set; }
public byte[] Bytes { get; set; }
public int ForeignKey { get; set; }
public static List<MyClass> AllMyClass { get; }
}
private List<Guid> GetIdentifiers(int foreignKey)
{
List<Guid> identifiers = new List<Guid>();
foreach (var item in MyClass.AllMyClass.FindAll(c => c.ForeignKey == foreignKey)
{
identifiers.Add(item.Identifier);
}
return identifiers;
}
Now, is there any faster/simpler way of implementing this with a lamda expression? As in, I’d like to condense the operation into one line:
List<Guid> identifiers = MyClass.AllMyClass.MagicExpression(c => c.ForeignKey == foreignKey);
In addition, would such as expression be possible for the .NET 2.0 framework?
I think that something like that will work :
More over, they’re no lambda expressions in .Net 2.0.