iqueryable

Group by hour in IQueryable

不羁岁月 提交于 2020-01-04 13:47:08
问题 In my project I receive some data from an SPS all x seconds. Every y minutes I archive the current Data in a database so I'm able to show statistics. The data I receive gets put in a model. Something like this but much more complex: public class Data { public DateTime ArchiveTime { get; set; } public float TempC { get; set; } public float CO2Percent { get; set; } } I have a repository for the database that returns all entries in a certain time span. See this code: // Context is my DbContext

Merging two iqueryables that are projected to one entity from different tables

亡梦爱人 提交于 2020-01-04 01:12:07
问题 I have tried This answer, This one and this one to merge two iqueryables. But I always receive the following error: The type 'Estudio' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order. I'm mapping from two different but similar Entity Framework Entities (EXAMEN and EXPLORACION) to my

Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code

喜你入骨 提交于 2020-01-03 08:53:08
问题 I am getting runtime error This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your

IQueryable vs IEnumerable: is IQueryable always better and faster?

 ̄綄美尐妖づ 提交于 2020-01-02 14:31:22
问题 If the IQueryable interface performs the query expression in the server rather than fetching all records like IEnumerable , why is IQueryable not replaced by IEnumerable where it can be faster and more efficient? DBSet<T> has two flavors of Where ( IQueryable and IEnumerable ). Is there a way to call the IEnumerable version because the IQueryable is called by default, without calling ToList() ? 回答1: If the IQueryable perform the query Expression in the server rather than fetching all records

Create dynamic LINQ query expression at runtime which translates into a parameterized SQL query

蹲街弑〆低调 提交于 2020-01-02 14:30:13
问题 I want to create my custom expression for IQueryable. Sample extension method: public static IQueryable<T> GetByIntTest<T>(this IQueryable<T> qSource, Expression<Func<T, int?>> field, int? value) { if (value == null) return qSource; ConstantExpression constantExpression = Expression.Constant(value, typeof(int?)); BinaryExpression binaryExpression = Expression.Equal(field.Body, constantExpression); var predicate = Expression.Lambda<Func<T, bool>>(binaryExpression, field.Parameters); return

How to formulate an IQueryable to query a recursive database table?

巧了我就是萌 提交于 2020-01-02 05:35:08
问题 I have a database table like this: Entity --------------------- ID int PK ParentID int FK Code varchar Text text The ParentID field is a foreign key with another record in the same table (recursive). So the structure represents a Tree. I'm trying to write a method to query this table and get 1 specific Entity based on a path. A path would be a string representing the Code properties of the Entity and the parent Entities. So an example path would be "foo/bar/baz" which means the one specific

Convert Predicate<T> to Expression<Func<T, bool>>

ⅰ亾dé卋堺 提交于 2020-01-01 09:33:14
问题 Is possible to convert a Predicate<T> to Expression<Func<T, bool>> in some way? I would like to use the next IQueryable function using the filters of the my ICollectionView: public static System.Linq.IQueryable<TSource> Where<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<System.Func<TSource, bool>> predicate) Thanks 回答1: In theory it is possible to convert a delegate 'back' to an expression, because you can request the emitted IL of a delegate, which

What is the best way to cast each item in a LINQ to Entities query to an interface?

两盒软妹~` 提交于 2020-01-01 09:23:22
问题 I have an entity object 'User' which implements 'IUser': IQueryable<User> users = Db.User; return users; But what I actually want to return is: IQueryable<IUser> So what is the best way to convert IQueryable<User> to IQueryable<IUser> without actually running the query? Right now I am doing this but it seems like a hack: IQueryable<IUser> users = Db.User.Select<User, IUser>(u => u); 回答1: Your "hacky" solution looks fine to me. 来源: https://stackoverflow.com/questions/1240087/what-is-the-best

Using an IQueryable in another IQueryable

十年热恋 提交于 2019-12-31 04:38:07
问题 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

Expose a repository as an IQueryable

醉酒当歌 提交于 2019-12-31 03:37:08
问题 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(); }