Note that there are some explanatory texts on larger screens.

plurals
  1. POC# LINQ subquery (On entities)
    primarykey
    data
    text
    <p>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. </p> <p>Classes:</p> <pre><code>public class FooType { public int Id { get; set; } public IList&lt;Foo&gt; Foos { get; set; } } public class Foo { public int FooTypeId { get; set; } public string CorrelationId { get; set; } } public class Bar { public string FooCorrelationId { get; set; } } </code></pre> <p>Usage: </p> <pre><code>IList &lt; FooType &gt; fooTypes = new List &lt; FooType &gt;(); // ... add a lot of FooTypes, each enriched with some Foos Bar bar = new Bar(){FooCorrelationId = "abcdef"}; Foo foo = fooTypes.Where( ft =&gt; ft.Foos.Where( f =&gt; f.CorrelationId == bar.FooCorrelationId ) ).First&lt;Foo&gt;(); </code></pre> <p>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:</p> <pre><code> foreach (FooType fooType in fooTypes) { Foo foo = fooType.Foos .Where(f =&gt; f.CorrelationId == bar.FooCorrelationId) .First(); } </code></pre> <p>Do you have any idea how to get that Foo I'm looking for, the one with CorrelationId "abcdef"? </p> <p>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?</p> <p>Thanks a lot in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload