<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.aspnetzone.de/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Peter Bucher : Any</title><link>http://www.aspnetzone.de/blogs/peterbucher/archive/tags/Any/default.aspx</link><description>Ordnungsbegriffe: Any</description><dc:language /><generator>CommunityServer 2.1 SP2 (Build: 61120.2)</generator><item><title>LINQ: Any, anyone? Range oder Repeat?</title><link>http://www.aspnetzone.de/blogs/peterbucher/archive/2009/09/01/linq-any-anyone-range-oder-repeat.aspx</link><pubDate>Tue, 01 Sep 2009 16:10:00 GMT</pubDate><guid isPermaLink="false">ce930855-ae9b-4fa4-8077-06a76071cc6a:212663</guid><dc:creator>Peter Bucher</dc:creator><slash:comments>0</slash:comments><comments>http://www.aspnetzone.de/blogs/peterbucher/comments/212663.aspx</comments><wfw:commentRss>http://www.aspnetzone.de/blogs/peterbucher/commentrss.aspx?PostID=212663</wfw:commentRss><description>&lt;P&gt;Mit &lt;A href="http://de.wikipedia.org/wiki/LINQ"&gt;LINQ&lt;/A&gt; sind unzählige Erweiterungsmethoden für &lt;A href="http://msdn.microsoft.com/de-de/library/9eekhta0.aspx"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/A&gt; hinzugekommen, die auch ausserhalb von LINQ Abfragen sehr nützlich sein können.&lt;/P&gt;
&lt;P&gt;Damit ich mir das merke und ihr evt. noch was neues kennenlernt, möchte ich zwei kurz vorstellen:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;A href="http://msdn.microsoft.com/de-de/library/system.linq.enumerable.any.aspx"&gt;Enumerable.Any()&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Kommt euch Code wie der folgende bekannt vor?&lt;/P&gt;
&lt;P&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;if&lt;/span&gt;(addresses.Count &amp;gt; 0) &lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color: Green;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;// … &lt;/span&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/P&gt;
&lt;P&gt;oder&lt;/P&gt;
&lt;P&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;if&lt;/span&gt;(addresses.Count == 0) &lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color: Green;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;// … &lt;/span&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/P&gt;
&lt;P&gt;Das kann jetzt folgendermassen umgeschrieben werden:&lt;/P&gt;
&lt;P&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;if&lt;/span&gt;(addresses.Any()) &lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color: Green;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;// … &lt;/span&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/P&gt;
&lt;P&gt;und&lt;/P&gt;
&lt;P&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;if&lt;/span&gt;(!addresses.Any()) &lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color: Green;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;// … &lt;/span&gt;&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/P&gt;
&lt;P&gt;Die Implementation der Any()-Methode holt sich den Enumerator der Auflistung und versucht den ersten Eintrag zu holen, wenn das klappt gibt sie true zurück, ansonsten false.&lt;/P&gt;
&lt;P&gt;Wenn Any() true zurückliefert, heisst das also, es ist mindestens ein Element vorhanden.&lt;/P&gt;
&lt;P&gt;PS: Darüber hinaus gibt es auch noch eine &lt;A href="http://msdn.microsoft.com/en-us/library/bb534972.aspx"&gt;zweite Überladung&lt;/A&gt;, der man Funktion einspeisen kann (Anonyme Methode, Lambda Ausdruck, …). Diese bestimmt dann, ob es mindestens ein Element hat, bei dem die Funktion true zurückliefert. &lt;BR&gt;Die Funktion nimmt ein Element vom Typ TSource entgegen und gibt einen bool zurück.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx"&gt;Enumerable.Range()&lt;/A&gt; und &lt;STRONG&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/bb348899.aspx"&gt;Enumerable.Repeat()&lt;/A&gt;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Schon mal Testdaten benötigt, oder eine Liste von Zahlen die dann durcheinandergewürfelt werden? &lt;BR&gt;Das schreibt man im einfachsten Fall dann in etwa so:&lt;/P&gt;
&lt;P&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;List&amp;lt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;int&lt;/span&gt;&amp;gt; numbers &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;=&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;int&lt;/span&gt;&amp;gt;(); &lt;br /&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;for&lt;/span&gt;(&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;int&lt;/span&gt; i=1; i &amp;lt;= 100; i++) &lt;br /&gt;{ &lt;br /&gt;    numbers.Add(i); &lt;br /&gt;} &lt;br /&gt;&lt;span style="color: Green;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;// …&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/P&gt;
&lt;P&gt;Mit Enumerable.Range() geht das viel einfacher:&lt;/P&gt;
&lt;P&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt; &lt;br /&gt;&lt;br /&gt;var numbers &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;=&lt;/span&gt; Enumerable.Range(1, 100);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: Green;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;// …&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/P&gt;
&lt;P&gt;Mit LINQ lässt sich ganz einfach eine gemischte Liste von Zahlen erstellen, zwei Zeilen braucht es dazu:&lt;/P&gt;
&lt;P&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;Random random &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;=&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;new&lt;/span&gt; Random(); &lt;br /&gt;var numbers &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;=&lt;/span&gt; Enumerable.Range(1, 100).OrderBy&amp;lt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;int&lt;/span&gt;, &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;int&lt;/span&gt;&amp;gt;(random.Next);&lt;/span&gt;&lt;/code&gt;&lt;/P&gt;
&lt;P&gt;Hier noch ein kleiner Hinweis: Um eine gleichmässige Verteilung einer solchen Liste zu erhalten, sollte ein Algorithmus wie bspw. &lt;A href="http://www.mycsharp.de/wbb2/thread.php?postid=198217#post198217"&gt;hier&lt;/A&gt; benutzt werden.&lt;/P&gt;
&lt;P&gt;An Enumerable.Repeat() hingegen kann im ersten Parameter eine Instanz übergeben werden, oder auch ein einfacher Wertetyp wie bspw. eine Zahl und eine Anzahl, wie häufig dieser Wert wiederholt werden soll. &lt;BR&gt;Schlussendlich bekommt man eine Auflistung zurück, das wars.&lt;/P&gt;&lt;img src="http://www.aspnetzone.de/aggbug.aspx?PostID=212663" width="1" height="1"&gt;</description><category domain="http://www.aspnetzone.de/blogs/peterbucher/archive/tags/Tipps/default.aspx">Tipps</category><category domain="http://www.aspnetzone.de/blogs/peterbucher/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://www.aspnetzone.de/blogs/peterbucher/archive/tags/Range/default.aspx">Range</category><category domain="http://www.aspnetzone.de/blogs/peterbucher/archive/tags/Repeat/default.aspx">Repeat</category><category domain="http://www.aspnetzone.de/blogs/peterbucher/archive/tags/Random/default.aspx">Random</category><category domain="http://www.aspnetzone.de/blogs/peterbucher/archive/tags/Shuffle/default.aspx">Shuffle</category><category domain="http://www.aspnetzone.de/blogs/peterbucher/archive/tags/Any/default.aspx">Any</category></item></channel></rss>