iqueryable

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

大憨熊 提交于 2019-12-01 11:27: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)

Possible to convert IQueryable<Derived> to IQueryable<Base>?

大兔子大兔子 提交于 2019-12-01 11:22:24
I know about covariance, and I know that in general it will not be possible in C# until v4.0. However I am wondering about a specific case. Is there some way of getting converting IQueryable<Derived> to IQueryable<Base> by somehow creating a wrapper class that does not actually perform a query, but can actually "pass through" a .Where<>() call? My use case is that I am trying to deal with a database schema that has many similar tables. Most of the fields are in common, and many of the common fields need to be queried on each table. I'm using LinqToSql. I was hoping to avoid duplicating all the

Possible to convert IQueryable<Derived> to IQueryable<Base>?

戏子无情 提交于 2019-12-01 09:19:46
问题 I know about covariance, and I know that in general it will not be possible in C# until v4.0. However I am wondering about a specific case. Is there some way of getting converting IQueryable<Derived> to IQueryable<Base> by somehow creating a wrapper class that does not actually perform a query, but can actually "pass through" a .Where<>() call? My use case is that I am trying to deal with a database schema that has many similar tables. Most of the fields are in common, and many of the common

Cannot convert IQueryable<> to IOrderedQueryable error

假装没事ソ 提交于 2019-12-01 02:47:59
I have the following LINQ code: var posts = (from p in db.Posts .Include("Site") .Include("PostStatus") where p.Public == false orderby p.PublicationTime select p); if (!chkShowIgnored.Checked) { posts = posts.Where(p => p.PostStatus.Id != 90); } That last line (the extra where) is giving me the error: Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IOrderedQueryable'. I'm not sure what this means... Why am I getting this error? It appeared once I added the "orderby" clause to the query, before that it compiled fine, so I have kind of a hunch of what is going on, but I

What instantiate-able types implementing IQueryable<T> are available in .Net 4.0?

天大地大妈咪最大 提交于 2019-12-01 02:39:44
Within the context of C# on .Net 4.0, are there any built-in objects that implement IQueryable<T> ? KeithS IQueryable objects are produced by Queryable Providers (ex. LINQ to SQL, LINQ to Entities/Entity Framework, etc). Virtually nothing you can instantiate with new in the basic .NET Framework implements IQueryable. IQueryable is an interface designed to be used to create Queryable providers, which allow the LINQ library to be leveraged against an external data store by building a parse-able expression tree. By nature, Queryables require a context - information regarding what exactly you're

Creating an expression from the string of a property name?

ぐ巨炮叔叔 提交于 2019-12-01 01:29:53
I am trying to create a query based on some JSON, I currently have the JSON parsed into a set of rules, each rule contains the name of the field, the type of comparison (=, > etc) and the value to compare. The issue I am having is getting it from that rule, to an IQueryable object, I am guessing I need to use reflection and somehow build the expression tree, but I'm not sure on the right approach... Assuming I have: public class Order : BaseEntity { public int OrderID{ get; set; } } and I have the rule which is: public class Rule { public string field { get; set; } public Operations op { get;

How to return empty IQueryable in an async repository method

梦想的初衷 提交于 2019-11-30 20:51:54
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).GetByNames(names).ToListAsync(); It will blow up if I pass in empty list or null because Enumerable.Empty

IQueryable (non generic) : missing Count and Skip ? it works with IQueryable<T>

被刻印的时光 ゝ 提交于 2019-11-30 20:40:00
i have an extension method which a person was really helpful to give me... it does an orderby on IQueryable ... but i wanted one to do a normal IQueryable (non generic) Here is the code, The count and Skip and i think Take are missing . public static IQueryable GetPage(this IQueryable query, int page, int pageSize, out int count) { int skip = (int)((page - 1) * pageSize); count = query.Count(); //COUNT DOESN'T EXIST return query.Skip(skip).Take((int)pageSize); // NEITHER SKIP } Here is the and it works perfectly no errors. public static IQueryable<T> GetPage<T>(this IQueryable<T> query, int

Creating an expression from the string of a property name?

笑着哭i 提交于 2019-11-30 19:53:30
问题 I am trying to create a query based on some JSON, I currently have the JSON parsed into a set of rules, each rule contains the name of the field, the type of comparison (=, > etc) and the value to compare. The issue I am having is getting it from that rule, to an IQueryable object, I am guessing I need to use reflection and somehow build the expression tree, but I'm not sure on the right approach... Assuming I have: public class Order : BaseEntity { public int OrderID{ get; set; } } and I

How to map an int to its enum description using AutoMapper during a queryable projection?

为君一笑 提交于 2019-11-30 16:15:16
问题 Here is the enum extension method to get its description attribute. public static string GetDescription(this Enum enumeration) { if (enumeration == null) throw new ArgumentNullException(); var value = enumeration.ToString(); var type = enumeration.GetType(); var descriptionAttribute = (DescriptionAttribute[]) type.GetField(value).GetCustomAttributes(typeof (DescriptionAttribute), false); return descriptionAttribute.Length > 0 ? descriptionAttribute[0].Description : value; } Here is the source