Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn an empty collection when Linq where returns nothing
    primarykey
    data
    text
    <p>I am using the below statement with the intent of getting all of the machine objects from the <code>MachineList</code> collection (type IEnumerable) that have a <code>MachineStatus</code> of <em>i</em>. The <code>MachineList</code> collection will not always contain machines with a status of <em>i</em>.</p> <p>At times when no machines have a <code>MachineStatus</code> of <em>i</em> I'd like to return an empty collection. My call to <code>ActiveMachines</code> (which is used first) works but <code>InactiveMachines</code> does not.</p> <pre><code>public IEnumerable&lt;Machine&gt; ActiveMachines { get { return Customer.MachineList .Where(m =&gt; m.MachineStatus == "a"); } } public IEnumerable&lt;Machine&gt; InactiveMachines { get { return Customer.MachineList .Where(m =&gt; m.MachineStatus == "i"); } } </code></pre> <p><strong>Edit</strong></p> <p>Upon further examination it appears that any enumeration of <code>MachineList</code> will cause subsequent enumerations of <code>MachineList</code> to throw an exeception: <code>Object reference not set to an instance of an object</code>.</p> <p>Therefore, it doesn't matter if a call is made to <code>ActiveMachines</code> or <code>InactiveMachines</code> as its an issue with the <code>MachineList</code> collection. This is especially troubling because I can break calls to <code>MachineList</code> simply by enumerating it in a Watch before it is called in code. At its lowest level <code>MachineList</code> implements <code>NHibernate.IQuery</code> being returned as an <code>IEnumerable</code>. What's causing <code>MachineList</code> to lose its contents after an initial enumeration?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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