IQueryable order by two or more properties

OrderBy(i => i.PropertyName).ThenBy(i => i.AnotherProperty) In OrderBy and ThenBy you have to provide keySelector function, which chooses key for sorting from object. So if you know property name only at runtime then you can make such function with Reflection like: var propertyInfo = i.GetType().GetProperty(“PropertyName”); var sortedList = myList.OrderBy(i => propertyInfo.GetValue(i, null)) But it will be … Read more

Automatically Compile Linq Queries

You can’t have extension methods invoked on anonymous lambda expressions, so you’ll want to use a Cache class. In order to properly cache a query you’ll also need to ‘lift’ any parameters (including your DataContext) into parameters for your lambda expression. This results in very verbose usage like: var results = QueryCache.Cache((MyModelDataContext db) => from … Read more

Should I return IEnumerable or IQueryable from my DAL?

It depends on what behavior you want. Returning an IList<T> tells the caller that they’ve received all of the data they’ve requested Returning an IEnumerable<T> tells the caller that they’ll need to iterate over the result and it might be lazily loaded. Returning an IQueryable<T> tells the caller that the result is backed by a … Read more

How do I mock IQueryable

I usually do exactly what you ended up doing in your test. When writing my tests I assume that the .Net library classes work correctly and don’t contain bugs, so I can use them in the tests. When I need a test list, collection, queryable, dictionary, etc. I just create the real thing and populate … Read more

The provider for the source IQueryable doesn’t implement IAsyncQueryProvider

I get stuck on this issue today and this lib resolve it for me https://github.com/romantitov/MockQueryable completely, please refer: Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. //1 – create a List<T> with test items var users = new List<UserEntity>() { new UserEntity{LastName = “ExistLastName”, DateOfBirth = DateTime.Parse(“01/20/2012”)}, … }; //2 – build mock by … Read more

How to merge two IQueryable lists

You’re not using the return value – just like all other LINQ operators, the method doesn’t change the existing sequence – it returns a new sequence. So try this: var list3 = list1.Concat(list2); or var list4 = list1.Union(list2); Union is a set operation – it returns distinct values. Concat simply returns the items from the … Read more