How do you transfer the execution of a Expression created by an IQueryable object to a IEnumerable?

喜欢而已 提交于 2019-12-22 11:01:54

问题


In my code I'd like to make my repositories IQueryable. This way, the criteria for selection will be a linq expression tree.

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).

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) ?

Thanks for your response,

Some precision

The client object of my repository will use my repository this way :

var result = from entry in MyRepository where entry.Product == "SomeProduct" select entry;

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.

Solution

The implementation of the solution is delegating call to IQueryable to my inmemory collection with AsQueryable.

public class MockRepository : IQueryable<DomainObject>
    {
        private List<DomainObject> inMemoryList = new List<DomainObject>();

        #region IEnumerable<DomainObject> Members

        public IEnumerator<DomainObject> 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
    }

回答1:


Use AsQueryable on your mocks. Now they're queryable and you can treat them like any other queryable.




回答2:


Can you use the extension method

.ToList<>


来源:https://stackoverflow.com/questions/503208/how-do-you-transfer-the-execution-of-a-expression-created-by-an-iqueryable-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!