0

I came across the linq query below and I am having trouble understanding it. It seems to have 2 from statements.

var foo = (from XmlNode listingSource in listingSources
          from idxRule in idxRules
          where idxRule.IDXRuleID == idxRuleId && idxRule.IsEnabled && idxRule.ListingSourceID == TrioXml.GetInt32Value(listingSource, "ListingSourceID")
          select TrioXml.GetInt32Value(listingSource, "ListingSourceID"));

Do 2 from statements denote inner and outer for loop? If I were to rewrite it as a loop, would it look like this?

var foo = new List<int>;
foreach (XmlNode listingSource in listingSources) {
  foreach (IDXRule idxRule in idxRules) {
    if (idxRule.IDXRuleID == idxRuleId && idxRule.IsEnabled && idxRule.ListingSourceID == TrioXml.GetInt32Value(listingSource, "ListingSourceID")) {
      foo.Add(TrioXml.GetInt32Value(listingSource, "ListingSourceID"));
    }
  }
}
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • See this answer: https://stackoverflow.com/a/6257269/8061994. Seems like you are right. –  Aug 17 '18 at 22:11
  • that would be same as a join statement ... well I mean *Cross Join* – Rahul Aug 17 '18 at 22:12
  • 1
    @Rahul: The select-many query form is somewhat more powerful than a cross join. The case `from x in xs from y in ys ...` is a straight-up Cartesian product. But LINQ lets you do `from x in xs from y in makeYs(x) ...` that is to say, *the collection of ys generated can depend on each specific x*. – Eric Lippert Aug 17 '18 at 22:28

0 Answers0