title: How to populate IEnumerable in Lambda expression?
body: <p>I just cannot get this to work, would appreciate if someone can help.</p>
  
  <p>So I get back an XML result from a database which looks like:</p>
  
  <pre><code>&lt;matches&gt;
    &lt;issuer client_name="MTR" score="6" match_list="MTR CORPORATION LIMITED"/&gt;
    &lt;issuer client_name="PEOPLE''S REPUBLIC OF CHINA" score="4"
            match_list="DEMOCRATIC PEOPLE'S REPUBLIC OF KOREA;GOVERNMENT OF THE
            HONG KONG SPECIAL ADMINISTRATIVE REGION OF THE PEOPLE'S REPUBLIC OF
            CHINA;MONGOLIAN PEOPLE'S REPUBLIC;PEOPLE'S DEMOCRATIC REPUBLIC OF
            ALGERIA;PEOPLE'S REPUBLIC OF CHINA"/&gt;
  &lt;/matches&gt;
  </code></pre>
  
  <p>From this XML I need to populate an object after doing some logic like matching the client_name I am getting back in the XML result to the one I have sent to database to get matches.</p>
  
  <pre><code>       XDocument.Load(new StringReader(
                                row.GetString(row.GetOrdinal("xml_ret"))))))
                   .Single().Descendants("matches")
                   .Select(x =&gt;
                       new Pool() {
                           Constituents = (IEnumerable&lt;Constituent&gt;)
                              //(... cannot work this how can IsReference populate)
                           //ClientName = x.Attribute("client_name").Value,
                           //Score = x.Attribute("score").Value,
                           //MatchList = x.Attribute("match_list").Value,
                       });
  </code></pre>
  
  <p>In a non-<a href="http://en.wikipedia.org/wiki/Language_Integrated_Query" rel="nofollow noreferrer">LINQ</a> manner I can populate the object something like this:</p>
  
  <pre><code>foreach (Constituent constituent in pool.Constituents)
          {
              if (!string.IsNullOrEmpty(constituent.Name)
                  &amp;&amp; string.IsNullOrEmpty(constituent.Curve))
              {
                  i++;
  
                  ConstituentMatch match = new ConstituentMatch();
                  ConstituentMatch.Group group =new ConstituentMatch.Group("High");
  
                  //High Match group
                  ICollection&lt;string&gt; curves = new List&lt;string&gt;();
                  curves.Add("EUR" + i);
                  curves.Add("USD" + i);
  
                  ICollection&lt;string&gt; names = new List&lt;string&gt;();
  
                  ConstituentMatch.Group.Entry entry =
                      new ConstituentMatch.Group.Entry(constituent.Name + " Ltd.",
                                                       curves);
                  group.Add(entry);
                  entry =
                      new ConstituentMatch.Group.Entry(constituent.Name + " Inc.",
                                                       curves);
                  group.Add(entry);
  
                  match.AddGroup(group);
  
              }
          }
  </code></pre>
  
  <p>But how can I do this using LINQ, as I am sure you can do it, I just cannot work it out.</p>
  
  <p>The constituent class looks like:</p>
  
  <pre><code>public sealed class Constituent
  {
      private string name;
  
      public string Name
      {
          get { return name; }
          set { name = value; }
      }
  
      public ConstituentMatch Match {get;set;}
  
      public Constituent(string name)
      {
          this.name = name;
      }
  
      public Constituent() : this(string.Empty) { }
  }
  </code></pre>
  
  <p>And constituent match class looks like this:</p>
  
  <pre><code>public sealed class ConstituentMatch
  {
      private readonly Dictionary&lt;string, Group&gt; matches = new Dictionary&lt;string, Group&gt;();
  
      public IEnumerable&lt;string&gt; GroupNames
      {
          get { return matches.Keys; }
      }
  
      public Group this[string name]
      {
          get { return matches[name]; }
      }
  
      public IEnumerable&lt;Group&gt; Groups
      {
          get { return matches.Values; }
      }
  
      public void AddGroup(Group group)
      {
          matches[group.Name] = group;
      }
  
      /// &lt;summary&gt;
      /// Match group e.g. Poor, High, All, Begins With
      /// &lt;/summary&gt;
      public sealed class Group
      {
          private readonly string name;
          private readonly ICollection&lt;Entry&gt; matches = new List&lt;Entry&gt;();
  
          public string Name
          {
              get { return name; }
          }
  
          public Group(string name)
          {
              this.name = name;
          }
  
          public void Add(Entry entry)
          {
              matches.Add(entry);
          }
  
          public override bool Equals(object obj)
          {
              bool result = false;
              if (obj is Group)
              {
                  Group other = obj as Group;
                  result = name == other.name;
              }
              return result;
          }
  
          public override int GetHashCode()
          {
              return name.GetHashCode();
          }
  
          public sealed class Entry
          {
              private string legalName;
              private IEnumerable&lt;string&gt; curves;
  
              private double notional     = double.NaN;
              private char seniriority    = 'N';
  
              public string LegalName
              {
                  get { return legalName; }
              }
  
              public IEnumerable&lt;string&gt; Curves
              {
                  get { return curves; }
              }
  
              public Entry(string legalName, IEnumerable&lt;string&gt; curves)
                         : this(legalName, curves, double.NaN, 'N') { }
  
              public Entry(string legalName,
                           IEnumerable&lt;string&gt; curves,
                           double notional,
                           char seniriority)
              {
                  this.legalName = legalName;
                  this.curves = curves;
                  this.notional = notional;
                  this.seniriority = seniriority;
              }
          }
      }
  }
  </code></pre>