Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving some confusion with LINQ
    text
    copied!<p>Some background info;</p> <ul> <li>LanguageResource is the base class</li> <li>LanguageTranslatorResource and LanguageEditorResource inherit from LanguageResource</li> <li>LanguageEditorResource defines an IsDirty property</li> <li>LanguageResourceCollection is a collection of LanguageResource</li> <li>LanguageResourceCollection internally holds LanguageResources in <code>Dictionary&lt;string, LanguageResource&gt; _dict</code></li> <li>LanguageResourceCollection.GetEnumerator() returns <code>_dict.Values.GetEnumerator()</code></li> </ul> <p>I have a LanguageResourceCollection _resources that contains only LanguageEditorResource objects and want to use LINQ to enumerate those that are dirty so I have tried the following. My specific questions are in bold.</p> <ol> <li><p><code>_resources.Where(r =&gt; (r as LanguageEditorResource).IsDirty)</code><br/><br/> neither Where not other LINQ methods are displayed by Intellisense but I code it anyway and am told "LanguageResourceCollection does not contain a definition for 'Where' and no extension method...".<br/><br/> <strong>Why does the way that LanguageResourceCollection implements IEnumerable preclude it from supporting LINQ?</strong></p></li> <li><p>If I change the query to<br/><br/> <code>(_resources as IEnumerable&lt;LanguageEditorResource&gt;).Where(r =&gt; r.IsDirty)</code><br/><br/> Intellisense displays the LINQ methods and the solution compiles. But at runtime I get an ArgumentNullException "Value cannot be null. Parameter name: source".<br/><br/> <strong>Is this a problem in my LINQ code?<br> Is it a problem with the general design of the classes?<br> How can I dig into what LINQ generates to try and see what the problem is?</strong></p></li> </ol> <p>My aim with this question is not to get a solution for the specific problem, as I will have to solve it now using other (non LINQ) means, but rather to try and improve my understanding of LINQ and learn how I can improve the design of my classes to work better with LINQ.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload