{"id":3855,"date":"2014-03-30T06:02:08","date_gmt":"2014-03-30T06:02:08","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-casting-question-from-ienumerable-to-custom-type-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:02:08","modified_gmt":"2014-03-30T06:02:08","slug":"c-casting-question-from-ienumerable-to-custom-type-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-casting-question-from-ienumerable-to-custom-type-collection-of-common-programming-errors\/","title":{"rendered":"C# casting question: from IEnumerable to custom type-Collection of common programming errors"},"content":{"rendered":"<p>I have a custom class called <code>Rows<\/code> that implements <code>IEnumerable<\/code>. I often use LINQ queries on <code>Rows<\/code> instances:<\/p>\n<pre><code>Rows rows = new Rows { row1, row2, row3 };\nIEnumerable particularRows = rows.Where(row =&gt; condition);\n<\/code><\/pre>\n<p>What I would like is to be able to do the following:<\/p>\n<pre><code>Rows rows = new Rows { row1, row2, row3 };\nRows particularRows = (Rows)rows.Where(row =&gt; condition);\n<\/code><\/pre>\n<p>However, I get a &#8220;System.InvalidCastException: Unable to cast object of type &#8216;WhereEnumerableIterator1[NS.Row]&#8217; to type &#8216;NS.Rows'&#8221;. I do have a <code>Rows<\/code> constructor taking <code>IEnumerable<\/code>, so I could do:<\/p>\n<pre><code>Rows rows = new Rows { row1, row2, row3 };\nRows particularRows = new Rows(rows.Where(row =&gt; condition));\n<\/code><\/pre>\n<p>This seems bulky, however, and I would love to be able to cast an <code>IEnumerable<\/code> to be a <code>Rows<\/code> since <code>Rows<\/code> implements <code>IEnumerable<\/code>. Any ideas?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a custom class called Rows that implements IEnumerable. I often use LINQ queries on Rows instances: Rows rows = new Rows { row1, row2, row3 }; IEnumerable particularRows = rows.Where(row =&gt; condition); What I would like is to be able to do the following: Rows rows = new Rows { row1, row2, row3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3855","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3855"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3855\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}