Hi all,
I am trying to use LINQ to query a list of objects wherever appropriate. Currently I am stuck on the syntax of a nested query which I hope you can help me with.
Classes:
public class FooType
{
public int Id { get; set; }
public IList<Foo> Foos { get; set; }
}
public class Foo
{
public int FooTypeId { get; set; }
public string CorrelationId { get; set; }
}
public class Bar
{
public string FooCorrelationId { get; set; }
}
Usage:
IList < FooType > fooTypes = new List < FooType >();
// ... add a lot of FooTypes, each enriched with some Foos
Bar bar = new Bar(){FooCorrelationId = "abcdef"};
Foo foo = fooTypes.Where( ft => ft.Foos.Where( f => f.CorrelationId == bar.FooCorrelationId ) ).First<Foo>();
This fails because the outer WHERE-expression is fed with something that does not provide a boolean return value. As you might guess, I intend to something like the following in LINQ:
foreach (FooType fooType in fooTypes)
{
Foo foo = fooType.Foos
.Where(f => f.CorrelationId == bar.FooCorrelationId)
.First();
}
Do you have any idea how to get that Foo I'm looking for, the one with CorrelationId "abcdef"?
And one more question for the benchmarkers : What about performance? Does LINQ optimize it's queries? Or is the result the same as an iteration over the objects like I did in my "something like" block?
Thanks a lot in advance!