Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you transfer the execution of a Expression created by an IQueryable object to a IEnumerable?
    primarykey
    data
    text
    <p>In my code I'd like to make my repositories IQueryable. This way, the criteria for selection will be a linq expression tree.</p> <p>Now if I want to mock my repository in theorie this is very easy : just implement the interface of my repository (which is also a IQueryable object).</p> <p>My mock repository implementation would be only a in memory collection, but my question is : Do you know an easy way to implement the IQueryable interface of my mock, to transfer the query to my in-memory collection (IEnumerable) ?</p> <p>Thanks for your response,</p> <p><strong><em>Some precision</em></strong></p> <p>The client object of my repository will use my repository this way :</p> <p>var result = from entry in MyRepository where entry.Product == "SomeProduct" select entry;</p> <p>What does ToList or AsEnumerable is to execute the query and return the result as a List or as a IEnumerable. But I have to implement the IQueryable interface on my repository, with a IQueryProvider which transform the expression in a call to a IEnumerable object.</p> <p><strong><em>Solution</em></strong></p> <p>The implementation of the solution is delegating call to IQueryable to my inmemory collection with AsQueryable.</p> <pre><code>public class MockRepository : IQueryable&lt;DomainObject&gt; { private List&lt;DomainObject&gt; inMemoryList = new List&lt;DomainObject&gt;(); #region IEnumerable&lt;DomainObject&gt; Members public IEnumerator&lt;DomainObject&gt; GetEnumerator() { return inMemoryList.GetEnumerator(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { return inMemoryList.GetEnumerator(); } #endregion #region IQueryable Members public Type ElementType { get { return inMemoryList.AsQueryable().ElementType; } } public Expression Expression { get { return inMemoryList.AsQueryable().Expression; } } public IQueryProvider Provider { get { return inMemoryList.AsQueryable().Provider; } } #endregion } </code></pre>
    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.
    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