iqueryable

How to return empty IQueryable in an async repository method

女生的网名这么多〃 提交于 2019-12-19 02:26:31
问题 Lets say I have a simple repository class, with one GetByNames method public class MyRepo { private readonly MyDbContext _db; public MyRepo(MyDbContext db) { _db = db; } public IQueryable<MyObject> GetByNames(IList<string> names) { if (names== null || !names.Any()) { return Enumerable.Empty<MyObject>().AsQueryable(); } return _db.MyObjects.Where(a => names.Contains(a.Name)); } } Now when I use it with async EntityFramework ToListAsync() extension var myObjects = awawit new MyRepo(_db)

LINQ to Entities does not recognize the method 'Method name' method

落爺英雄遲暮 提交于 2019-12-18 16:30:32
问题 I'm having a similar problem that was asked here: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression I'm trying to paginate my source, but in my case, I can't put the result of GetPropertyValue in a variable, because I need x to do that: public IEnumerable<TModel> Paginate(IQueryable<TModel> source, ref int totalPages, int pageIndex, int pageSize, string sortfield, SortDirection? sortdir) { totalPages

Access DataContext behind IQueryable

心不动则不痛 提交于 2019-12-18 13:03:06
问题 Is it possible to access the DataContext object behind an IQueryable? If so, how? 回答1: DataContext is specific to LINQ to SQL, so presumably you're talking about LINQ to SQL queries? If so, there's no safe way to do this - you have to resort to a hack such as using reflection to retrieve the private "context" field of the underlying DataQuery object: static DataContext GetContext (IQueryable q) { if (!q.GetType().FullName.StartsWith ("System.Data.Linq.DataQuery`1")) return null; var field = q

C# How to serialize system.linq.expressions?

不羁岁月 提交于 2019-12-18 09:07:08
问题 I am working on winRT and entity framework (to SQL), the layer that communicates between them is WCF Service. In the entity framework I am using the Repository Pattern and I have the method: public IQueryable<User> GetBySearch(Expression<Func<User, bool>> search) { return this.Context.Users.Where(search); } Everything works fine, but when I add it to WCF [OperationContract] IQueryable<User> GetUserBySearch(Expression<Func<User, bool>> search); and: public IQueryable<User> GetUserBySearch

Get all 'where' calls using ExpressionVisitor

蹲街弑〆低调 提交于 2019-12-18 04:24:22
问题 I have a query, like such: var query = from sessions in dataSet where (names.Contains(sessions.Username)) where (sessions.Login.TimeOfAction == dt) select new { sessions.Username, sessions.Login, sessions.Logout, sessions.Duration }; I want to implement an ExpressionVisitor to extract BOTH the where clauses as Lambda's, but so far have only been able to get the first using a class called 'InnermostWhereFinder' that comes from the MSDN tutorial on a custom query provider for the TerraServer

IQueryable OfType<T> where T is a runtime Type

狂风中的少年 提交于 2019-12-17 23:30:56
问题 I need to be able to get something similar to the following to work: Type type = ??? // something decided at runtime with .GetType or typeof; object[] entityList = context.Resources.OfType<type>().ToList(); Is this possible? I am able to use .NET 4 if anything new in that allows this. 回答1: You can call it by reflection: MethodInfo method = typeof(Queryable).GetMethod("OfType"); MethodInfo generic = method.MakeGenericMethod(new Type[]{ type }); // Use .NET 4 covariance var result =

The method 'Where' cannot follow the method 'Select' or is not supported

China☆狼群 提交于 2019-12-17 21:07:32
问题 Why am I getting: The method 'Where' cannot follow the method 'Select' or is not supported. Try writing the query in terms of supported methods or call the 'AsEnumerable' or 'ToList' method before calling unsupported methods. ...when using the WHERE clause, like when calling: XrmServiceContext.CreateQuery<Contact>().Project().To<Person>().Where(p => p.FirstName == "John").First(); ? This works: XrmServiceContext.CreateQuery<Contact>().Project().To<Person>().First(); Also this works:

The provider for the source IQueryable doesn't implement IAsyncQueryProvider

六月ゝ 毕业季﹏ 提交于 2019-12-17 20:47:05
问题 I have some codes like below, I want to write unit tests my method. But I'm stuck in async methods. Can you help me please ? public class Panel { public int Id { get; set; } [Required] public double Latitude { get; set; } public double Longitude { get; set; } [Required] public string Serial { get; set; } public string Brand { get; set; } } public class CrossSolarDbContext : DbContext { public CrossSolarDbContext() { } public CrossSolarDbContext(DbContextOptions<CrossSolarDbContext> options) :

how can I convert IQueryable<string> to string?

徘徊边缘 提交于 2019-12-17 19:34:02
问题 I do a sql query which returns a string - service name. this is the query: IQueryable<string> query = from Comp in ServiceGroupdb.ServiceGroupes where (Comp.GroupID == groupID) select Comp.Name; How do i get the string out of the query? 回答1: LINQ always returns a sequence, so you have to retrieve the item out of it. If you know that you will have only one result, use Single() to retrieve that item. var item = (from Comp in ServiceGroupdb.ServiceGroupes where (Comp.GroupID == groupID) select

How to merge two IQueryable lists

这一生的挚爱 提交于 2019-12-17 16:26:44
问题 I want to merge the records of two IQueryable lists in C#. I try IQueryable<MediaType> list1 = values; IQueryable<MediaType> list2 = values1; obj.Concat(obj1); and IQueryable<MediaType> list1 = values; IQueryable<MediaType> list2 = values1; obj.Union(obj1); but if list1 is empty then the resultant list is also empty. In my case either list1 can be empty but list2 can have records. How should i merge them? 回答1: You're not using the return value - just like all other LINQ operators, the method