<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eric Webb &#187; linq</title>
	<atom:link href="http://ericcwebb.com/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://ericcwebb.com</link>
	<description>Work, life, and everything else...</description>
	<lastBuildDate>Tue, 31 Aug 2010 14:57:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>LINQ to XML subquery</title>
		<link>http://ericcwebb.com/2008/06/20/linq-to-xml-subquery/</link>
		<comments>http://ericcwebb.com/2008/06/20/linq-to-xml-subquery/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 20:27:12 +0000</pubDate>
		<dc:creator>Eric Webb</dc:creator>
				<category><![CDATA[general]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://ericcwebb.com/?p=12</guid>
		<description><![CDATA[I didn&#8217;t find a clear example online about how to do a subquery of an element, so I thought I would share. First, here&#8217;s the XML I&#8217;m reading: &#38;lt;response&#38;gt; &#38;lt;name&#38;gt;Test&#38;lt;/name&#38;gt; &#38;lt;states&#38;gt; &#38;lt;state&#38;gt;OH&#38;lt;/state&#38;gt; &#38;lt;state&#38;gt;MI&#38;lt;/state&#38;gt; &#38;lt;/states&#38;gt; &#38;lt;/response&#38;gt; I want to use LINQ to parse through the responses and create new objects for each one of them.  The [...]]]></description>
			<content:encoded><![CDATA[<p>I didn&#8217;t find a clear example online about how to do a subquery of an element, so I thought I would share.</p>
<p>First, here&#8217;s the XML I&#8217;m reading:</p>
<pre><code class="html">&amp;lt;response&amp;gt; 
    &amp;lt;name&amp;gt;Test&amp;lt;/name&amp;gt; 
    &amp;lt;states&amp;gt; 
        &amp;lt;state&amp;gt;OH&amp;lt;/state&amp;gt; 
        &amp;lt;state&amp;gt;MI&amp;lt;/state&amp;gt; 
    &amp;lt;/states&amp;gt; 
&amp;lt;/response&amp;gt;</code></pre>
<p>I want to use LINQ to parse through the responses and create new objects for each one of them.  The object I created to hold the info is below:</p>
<pre><code class="csharp">[Serializable()]
    public class User
    {
        public string userName { get; set; }a
        public List&lt;string&gt; states { get; set; }

        public User(string UserName,List&lt;string&gt; States)
        {
            userName = UserName;
            states = States;
        }
    }</code>
</pre>
<p>I was at a loss at how to get the state elements into my &#8220;States&#8221; list.  Here&#8217;s the query I ended up using:</p>
<pre><code class="csharp">var responses = from response in loginResult.Descendants("response")
      select new
      {
            authenticationToken = response.Element("authentication_token").Value,
            states = (from st in response.Descendants("states").Elements()
                         select st.Value).ToList(),

      };</code>
</pre>
<p>It was obvious after I got it and I probably should have been able to figure it out quicker, but such as life when learning something new.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericcwebb.com/2008/06/20/linq-to-xml-subquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
