iqueryable

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

最后都变了- 提交于 2019-11-30 13:51:14
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 = (int)Math.Ceiling(source.Count() / (double)pageSize); if (sortdir == SortDirection.Descending) {

Access DataContext behind IQueryable

一世执手 提交于 2019-11-30 08:45:00
Is it possible to access the DataContext object behind an IQueryable? If so, how? 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.GetType().GetField ("context", BindingFlags.NonPublic | BindingFlags.Instance); if (field == null) return

Implement IQueryable wrapper to translate result objects

橙三吉。 提交于 2019-11-30 04:53:39
Update 2013-08-22: After having a look at the 'Building an IQueryable provider series' (thanks for the link!) I got a bit further. I updated the code accordingly. It is still not fully working though. If I understand the tutorial correctly, the GetEnumerator is invoked in case multiple elements are requested (e.g. by a ToList() call on the queryable, or any aggregation function). So all the GetEnumerator implementation of the wrapper has to do is call an Execute on the provider and pass the queryable's expression. In the other case, if only a single element is requested, Execute is called

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

别来无恙 提交于 2019-11-30 04:17:33
问题 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

Repository / IQueryable / Query Object

∥☆過路亽.° 提交于 2019-11-29 22:26:19
I am building a repository and I've seen in many places 2 reasons not to expose IQueryable outside the repository. 1) The first is because different LINQ providers could behave differently, and this difference should be contained within the repository. 2) The second is to prevent service level developers from modifying the database query such that it accidentally causes performance issues. I guess issue 2 can only be prevented by keeping all query logic within the repository and not allowing any form of external query building? But that does seem a bit impractical to me. Issue 1 would seem to

Should I return IEnumerable<T> or IQueryable<T> from my DAL?

梦想的初衷 提交于 2019-11-29 19:32:59
I know this could be opinion, but I'm looking for best practices. As I understand, IQueryable<T> implements IEnumerable<T> , so in my DAL, I currently have method signatures like the following: IEnumerable<Product> GetProducts(); IEnumerable<Product> GetProductsByCategory(int cateogoryId); Product GetProduct(int productId); Should I be using IQueryable<T> here? What are the pros and cons of either approach? Note that I am planning on using the Repository pattern so I will have a class like so: public class ProductRepository { DBDataContext db = new DBDataContext(<!-- connection string -->);

IQueryable vs IQueryable<T>

会有一股神秘感。 提交于 2019-11-29 14:44:23
I'm simply wondering why there is a IQueryable<T> version without the generic capability ? The generic IQueryable<T> is the one you use most often in method signatures and the like. The non-generic IQueryable exist primarily to give you a weakly typed entry point primarily for dynamic query building scenarios . by Matt Warren from LINQ: Building an IQueryable Provider - Part I You should use generic IQueryable<T> everywhere it's possible. neontapir I imagine it's the same reason as Jon Skeet gives in Difference between IEnumerable and IEnumerable<T>? , to allow use in a foreach loop. IQuerable

Determine the position of an element in an IQueryable

こ雲淡風輕ζ 提交于 2019-11-29 14:17:54
问题 I have a IQueryable which is ordered by some condition. Now I want to know the position of a particular element in that IQueryable. Is there a linq expression to get that. Say for example there are 10 elements in the IQueryable and the 6th element matches a condition, I want to get the number 6. 回答1: First select each item with its index, then filter the items, and finally extract the original index: var result = orderedList .Select((x, i) => new { Item = x, Index = i }) .Where(itemWithIndex

IQueryable Repository with StructureMap (IoC) - How do i Implement IDisposable?

一笑奈何 提交于 2019-11-29 04:48:44
If i have the following Repository: public IQueryable<User> Users() { var db = new SqlDataContext(); return db.Users; } I understand that the connection is opened only when the query is fired: public class ServiceLayer { public IRepository repo; public ServiceLayer(IRepository injectedRepo) { this.repo = injectedRepo; } public List<User> GetUsers() { return repo.Users().ToList(); // connection opened, query fired, connection closed. (or is it??) } } If this is the case, do i still need to make my Repository implement IDisposable? The Visual Studio Code Metrics certainly think i should. I'm

Extend IQueryable<T> Where() as OR instead of AND relationship

可紊 提交于 2019-11-29 04:19:00
I am using my own extension methods of IQueryable<> to create chainable queries such as FindAll().FindInZip(12345).NameStartsWith("XYZ").OrderByHowIWantIt() etc. which then on deferred execution creates a single query based on my extension methods chain. The problem with this though, is that all Where's in the extension chain (FindXYZ, FindInZip etc.) will always combine as AND which means I can't do something like this: FindAll().FirstNameStartsWith("X").OrLastNameStartsWith("Z") because I don't know how I can inject the OR in a separate Where method. Any idea how I can solve this? additional