<?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>Code Reference &#187; linq</title>
	<atom:link href="http://sullivan.net/blog/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://sullivan.net/blog</link>
	<description>A collection of code for my reference (and perhaps other people too)</description>
	<lastBuildDate>Wed, 25 Jan 2012 17:53:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using Link to sort listbox content</title>
		<link>http://sullivan.net/blog/2009/07/usiong-link-to-sort-listbox-content/</link>
		<comments>http://sullivan.net/blog/2009/07/usiong-link-to-sort-listbox-content/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 07:05:23 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=52</guid>
		<description><![CDATA[This is a great way to sort lists for listboxes and other UIElements. LINQ will only sort specific types of lists. // Must use Linq using System.Linq; . . . // Your list ObservableCollection&#60;MyObject&#62; myList = new ObservableCollection&#60;MyObject&#62;(); // Your listbox ListBox listBox = new ListBox(); // Sorting is done like this listBox.ItemsSource = (from [...]]]></description>
			<content:encoded><![CDATA[<p>This is a great way to sort lists for listboxes and other UIElements.</p>
<p>LINQ will only sort specific types of lists.</p>
<pre class="brush: c#">
// Must use Linq
using System.Linq;
.
.
.
// Your list
ObservableCollection&lt;MyObject&gt; myList = new ObservableCollection&lt;MyObject&gt;();
// Your listbox
ListBox listBox = new ListBox();
// Sorting is done like this
listBox.ItemsSource = (from MyObject o in myList
                       where o.Name.ToLower().Contains(textboxNameSearch.Text.ToLower())
                       || o.Address.ToLower().Contains(textboxAddressSearch.Text.ToLower())
                       select o);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/07/usiong-link-to-sort-listbox-content/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>using AsQueryable().Where() to filter data</title>
		<link>http://sullivan.net/blog/2009/07/using-asqueryable-where-to-filter-data/</link>
		<comments>http://sullivan.net/blog/2009/07/using-asqueryable-where-to-filter-data/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 06:53:03 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=13</guid>
		<description><![CDATA[Sorting lists using AsQueryable: This uses both System.Linq and System.Linq.Dynamic List testList = new List() { new TestClass { ID = 1, Name = &#34;David&#34;, Other = &#34;other&#34; }, new TestClass { ID = 2, Name = &#34;James&#34;, Other = &#34;other&#34; }, new TestClass { ID = 3, Name = &#34;Darren&#34;, Other = &#34;some&#34; }, [...]]]></description>
			<content:encoded><![CDATA[<p>Sorting lists using AsQueryable:</p>
<p>This uses both System.Linq and System.Linq.Dynamic</p>
<pre class="brush: c#">
List testList = new List()
{
    new TestClass
        {
            ID = 1,
            Name = &quot;David&quot;,
            Other = &quot;other&quot;
        },
    new TestClass
        {
            ID = 2,
            Name = &quot;James&quot;,
            Other = &quot;other&quot;
        },
    new TestClass
        {
            ID = 3,
            Name = &quot;Darren&quot;,
            Other = &quot;some&quot;
        },
};

string searchString = &quot; (Name == \&quot;James\&quot; AND Other == \&quot;other\&quot;) Or (Name == \&quot;Darren\&quot;) &quot;;

var queryResult = testList.AsQueryable().Where(searchString);
</pre>
<p>The extra Class:</p>
<pre class="brush: c#">
class TestClass
{
    public int ID {get; set;}
    public string Name {get; set;}
    public string Other {get; set;}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/07/using-asqueryable-where-to-filter-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Casting a List of objects</title>
		<link>http://sullivan.net/blog/2009/07/casting-a-list-of-objects/</link>
		<comments>http://sullivan.net/blog/2009/07/casting-a-list-of-objects/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 06:51:11 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=10</guid>
		<description><![CDATA[Casting a list of objects is as simple as (using Linq): List masterList = new List(); var list = masterList.Cast(); If you don't know the type to cast to, it becomes a bit difficult. MethodInfo m = typeof(System.Linq.Enumerable).GetMethod(&#34;Cast&#34;).MakeGenericMethod(new Type[] { typeof(Href) }); var result = m.Invoke(typeof(System.Linq.Enumerable), new object[] { masterList }); This works just fine. [...]]]></description>
			<content:encoded><![CDATA[<p>Casting a list of objects is as simple as (using Linq):</p>
<pre class="brush: c#">
List masterList = new List();
var list = masterList.Cast();
</pre>
<p>If you don't know the type to cast to, it becomes a bit difficult.</p>
<pre class="brush: c#">
MethodInfo m = typeof(System.Linq.Enumerable).GetMethod(&quot;Cast&quot;).MakeGenericMethod(new Type[] { typeof(Href) });
var result = m.Invoke(typeof(System.Linq.Enumerable), new object[] { masterList });
</pre>
<p>This works just fine.<br />
However, the result is not a list. This would not be a problem normally. You would just add a .ToList() to the end.</p>
<pre class="brush: c#">
List masterList = new List();
var list = masterList.Cast().ToList();
</pre>
<p>This does not work the generic way. I thought another Invoke would do the trick. I could not figure out to do that.<br />
Instead I opted for a more simple option. I created a generic method as such:</p>
<pre class="brush: c#">
public void newMethod()
{
    var list = masterList .Cast().ToList();
}
</pre>
<p>To cast a type list you just add the type.</p>
<pre class="brush: c#">
List&lt;object&gt; oList = new List&lt;object&gt;();
List&lt;string&gt; sList = new List&lt;string&gt;();

sList = oList.Cast&lt;string&gt;.ToList();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/07/casting-a-list-of-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

