iqueryable

How do I mock IQueryable<T>

爱⌒轻易说出口 提交于 2019-12-03 01:02:23
I am creating a repository that exposes IQueryable. What is the best way to mock this out for my unit testing? Since I am using RhinoMocks for the rest of my mock objects, I tried to do the following: IQueryable<MyObject> QueryObject = MockRepository.GenerateStub<IQueryable<MyObject>>(); This doesn't work though so I tried doing this: IQueryable<MyObject> QueryObject = (new List<MyObject> { new MyObject() }).AsQueryable(); Is there a better way to do this, or have any other mocking frameworks built support for IQueryable in? My repository interface looks like this: public interface IRepository

IEnumerable<T> and IQueryable<T> clarification?

旧时模样 提交于 2019-12-02 22:52:51
After reading this question, I need to clear up some things. IQueryable<Customer> custs = from c in db.Customers where c.City == "<City>" select c; IEnumerable<Customer> custs = from c in db.Customers where c.City == "<City>" select c; Questions: 1) Is it ok to say that: in the first query the SQLServer is running the whole operation including where clause and returning ONLY the relevant rows - while the second one does SELECT * ... and returns all rows into C# and THEN filters ? 2) What about if I have a collection merely - in memory. ( var lstMyPerson = new List<MyPerson>() ) IQueryable

How can I write a clean Repository without exposing IQueryable to the rest of my application?

拟墨画扇 提交于 2019-12-02 16:47:01
So, I've read all the Q&A's here on SO regarding the subject of whether or not to expose IQueryable to the rest of your project or not (see here , and here ), and I've ultimately decided that I don't want to expose IQueryable to anything but my Model. Because IQueryable is tied to certain persistence implementations I don't like the idea of locking myself into this. Similarly, I'm not sure how good I feel about classes further down the call chain modifying the actual query that aren't in the repository. So, does anyone have any suggestions for how to write a clean and concise Repository

LINQ-to-entities casting issue

只谈情不闲聊 提交于 2019-12-02 10:30:59
问题 I'm trying to filter a LINQ-to-entities query in a generic way, but I keep getting an error. Here is a piece of code: private IQueryable<T> FilterDeletedEntities<T>(IQueryable<T> entities) { if (typeof(IDeletable).IsAssignableFrom(typeof(T))) { var deletableEntities = (IQueryable<IDeletable>)entities; deletableEntities = deletableEntities.Where(entity => !entity.Deleted); entities = (IQueryable<T>)deletableEntities; } return entities; } Basically I'm trying to filter out deleted entities (i.e

LINQ-to-entities casting issue

孤人 提交于 2019-12-02 06:04:47
I'm trying to filter a LINQ-to-entities query in a generic way, but I keep getting an error. Here is a piece of code: private IQueryable<T> FilterDeletedEntities<T>(IQueryable<T> entities) { if (typeof(IDeletable).IsAssignableFrom(typeof(T))) { var deletableEntities = (IQueryable<IDeletable>)entities; deletableEntities = deletableEntities.Where(entity => !entity.Deleted); entities = (IQueryable<T>)deletableEntities; } return entities; } Basically I'm trying to filter out deleted entities (i.e. 'Deleted' field is 'true'), if and only if the entity is IDeletable (i.e. it has the 'Deleted' field)

Using an IQueryable in another IQueryable

…衆ロ難τιáo~ 提交于 2019-12-02 04:04:06
I've an extension method, which returns an IQueryable, to get company products, I just want to use it in a IQueryable as a subquery, public static class DBEntitiesCompanyExtensions { public static IQueryable<Product> GetCompanyProducts(this DBEntities db, int companyId) { return db.Products.Where(m => m.CompanyId == companyId); } } And this is how I call it, using(var db = new DBEntities()) { var query = db.Companies.Select(m => new { CompanyName = m.Name, NumberOfProducts = db.GetCompanyProducts(m.CompanyId).Count() }); } I expected it to works beacuse my extension methods returns an

Expose a repository as an IQueryable

时光总嘲笑我的痴心妄想 提交于 2019-12-01 22:59:14
I'd like to expose a Repository as an 'IQueryable' type. The repository uses Linq to NHibernate to communicate with the database. Can anyone point me at an example implementation? For example, what would the corresponding 'GetEnumerator()' implementation on my repository look like? Edit: Would something like this be appropriate? public class MyTypeRepository : IEnumerable<MyType> { IEnumerator<MyType> IEnumerable<MyType>.GetEnumerator() { return Session.Linq<MyType>().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable<MyType>)this).GetEnumerator(); } } This is a

IQueryable.Distinct() vs List.Distinct()

旧时模样 提交于 2019-12-01 19:39:36
问题 I have a linq query that I am using Distinct() on. If I just call Distinct() without converting to a List then it does not return a distinct list - it still contains duplicates. However if I convert to a List and then call Distinct() - it works as expected and I only get unique objects. I'm using Telerik ORM and the objects being returned are the class representing one of the tables in the database. var uniqueUsers = (from u in Database.Users select u).Distinct(); The code above does not

LINQ: How to remove element from IQueryable<T>

纵饮孤独 提交于 2019-12-01 15:12:31
问题 How do you loop through IQueryable and remove some elements I don't need. I am looking for something like this var items = MyDataContext.Items.Where(x => x.Container.ID == myContainerId); foreach(Item item in items) { if(IsNotWhatINeed(item)) items.Remove(item); } Is it possible? Thanks in advance 回答1: You should be able to query that further as in this var filtered = items.Where(itm => IsWhatINeed(itm)); Also notice the subtle change in the boolean function to an affirmative rather than a

IQueryable<T> with EntityObject using Generics & Interfaces (Possible?)

天涯浪子 提交于 2019-12-01 12:04:39
I have a search repository for EntityFramework 4.0 using LinqKit with the following search function: public IQueryable<T> Search<T>(Expression<Func<T, bool>> predicate) where T : EntityObject { return _unitOfWork.ObjectSet<T>().AsExpandable().Where(predicate); } And another class which uses the IQueryable return value to subset the query in ways that are not possible using the Boolean LinqKit PredicateBuilder expressions: public IQueryable<T> SubsetByUser<T>(IQueryable<T> set, User user) where T : EntityObject { return set.Join(_searcher.Search<Metadatum>((o) => o.UserGUID == user.GUID), arc =