如何理解此Linq查询?

时间:2018-08-17 22:07:47

标签: c# linq

我遇到了下面的linq查询,无法理解它。它似乎有2条from语句。

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"));

2个from语句是否表示内部和外部for循环?如果我将其重写为循环,会是这样吗?

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"));
    }
  }
}

0 个答案:

没有答案