I have this code:
class A
{
public string Prop1;
public string Prop2;
public List<B> Prop3;
}
class B
{
public string Value1;
public int Value2;
public double Value3;
}
and have List of A
and I neeed to get Value1
from B
if Value2
from B
equals to some string. I'd like to use lambda. How can I get it?
Here is the code when I use foreach
:
//I have listA of type List<A>
foreach(var a in listA)
{
foreach(bar b in a.Prop3
{
if(b.Value1=="some string")
{
return b.Value2;
}
}
}
How to use lambda without foreach
?
You can do something like this:
return listA
.Select(a =>
a.Prop3.FirstOrDefault(b => b.Value1 == "some string"))
.FirstOrDefault();