I have the following object structure:
public class A
{
public string ID { get; set; }
public IList<B> Values { get; set; }
}
public class B
{
public string Code { get; set; }
public string DisplayName { get; set; }
}
public List<A> IDs;
I would like to use Linq to query B and return a single instance of A with the single element of B in values. Is that possible? I currently do this with a foreach but I am thinking Linq would be neater.
foreach (A a in IDs)
{
foreach (B b in a.Values)
{
if (b.Code == code)
{
return (new A()
{
ID = a.ID,
Values = new List<B>()
{
new B()
{
Code = b.Code,
DisplayName = b.DisplayName
}
}
});
}
}
}