<p>I am using LINQ to query a generic dictionary and then use the result as the datasource for my ListView (WebForms).</p>

<p>Simplified code:</p>

<pre><code>Dictionary&lt;Guid, Record&gt; dict = GetAllRecords();
myListView.DataSource = dict.Values.Where(rec =&gt; rec.Name == "foo");
myListView.DataBind();
</code></pre>

<p>I thought that would work but in fact it throws a <strong>System.InvalidOperationException</strong>: </p>

<blockquote>
  <p>ListView with id 'myListView' must
  have a data source that either
  implements ICollection or can perform
  data source paging if AllowPaging is
  true.</p>
</blockquote>

<p>In order to get it working I have had to resort to the following:</p>

<pre><code>Dictionary&lt;Guid, Record&gt; dict = GetAllRecords();
List&lt;Record&gt; searchResults = new List&lt;Record&gt;();

var matches = dict.Values.Where(rec =&gt; rec.Name == "foo");
foreach (Record rec in matches)
    searchResults.Add(rec);

myListView.DataSource = searchResults;
myListView.DataBind();
</code></pre>

<p>Is there a small gotcha in the first example to make it work?</p>

<p>(Wasn't sure what to use as the question title for this one, feel free to edit to something more appropriate)</p>

[{'answer_id': 31710, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 6, 'selected': True, 'text': '<p>Try this:</p>\n\n<pre><code>var matches = dict.Values.Where(rec =&gt; rec.Name == "foo").ToList();\n</code></pre>\n\n<p>Be aware that that will essentially be creating a new list from the original Values collection, and so any changes to your dictionary won\'t automatically be reflected in your bound control.</p>\n'}, {'answer_id': 31712, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 1, 'selected': False, 'text': '<pre><code>myListView.DataSource = (List&lt;Record&gt;) dict.Values.Where(rec =&gt; rec.Name == "foo");\n</code></pre>\n'}, {'answer_id': 31713, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>You might also try:</p>\n\n<pre><code>var matches = new List&lt;Record&gt;(dict.Values.Where(rec =&gt; rec.Name == "foo"));\n</code></pre>\n\n<p>Basically generic collections are very difficult to cast directly, so you really have little choice but to create a new object.</p>\n'}, {'answer_id': 32016, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 3, 'selected': False, 'text': '<p>I tend to prefer using the new Linq syntax:</p>\n\n<pre><code>myListView.DataSource = (\n    from rec in GetAllRecords().Values\n    where rec.Name == "foo"\n    select rec ).ToList();\nmyListView.DataBind();\n</code></pre>\n\n<p>Why are you getting a dictionary when you don\'t use the key? You\'re paying for that overhead.</p>\n'}, {'answer_id': 38828112, 'author': 'Elias MP', 'author_id': 5770216, 'author_profile': 'https://Stackoverflow.com/users/5770216', 'pm_score': 0, 'selected': False, 'text': '<p>Just adding knowledge the next sentence doesn´t recover any data from de db. Just only create the query (for that it is iqueryable type). For launching this query you must to add .ToList() or .First() at the end.</p>\n\n<pre><code>dict.Values.Where(rec =&gt; rec.Name == "foo")\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31708', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/202/']