iqueryable

Implement IQueryable wrapper to translate result objects

和自甴很熟 提交于 2019-11-29 02:27:15
问题 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

IQueryable vs. IEnumerable in the repository pattern , lazy loading

假装没事ソ 提交于 2019-11-29 01:26:25
I have read some articles that state that IEnumerable used to mimic stored procedures or restrict your database. Lost lazy loading ability on the external provider. Where as IQueryable to give developers more flexibility. Lazy loading is there. In terms of performance, both consume a significant amount of performance .. so which one is more preferable? Robert Harvey From the perspective of a Repository Pattern, you can think of it this way: Use an eager loading IEnumerable when you want to pass an entire list to the client in one go. They can still add linq clauses, but the client does not

IQueryable OfType<T> where T is a runtime Type

青春壹個敷衍的年華 提交于 2019-11-28 19:15:34
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. 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 = (IEnumerable<object>) generic.Invoke (null, new object[] { context.Resources }); object[] array = result.ToArray(); An

Is there a C# LINQ syntax for the Queryable.SelectMany() method?

99封情书 提交于 2019-11-28 18:30:41
When writing a query using C# LINQ syntax, is there a way to use the Queryable.SelectMany method from the keyword syntax? For string[] text = { "Albert was here", "Burke slept late", "Connor is happy" }; Using fluent methods I could query var tokens = text.SelectMany(s => s.Split(' ')); Is there a query syntax akin to var tokens = from x in text selectmany s.Split(' ') Yes, you just repeat the from ... in clause: var words = from str in text from word in str.Split(' ') select word; You can use a Compound from Clause : var tokens = from s in text from x in s.Split(' ') select x; Your query

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

橙三吉。 提交于 2019-11-28 15:06:35
问题 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

Trouble with building a C# EntityFramework IQueryable Expression

落花浮王杯 提交于 2019-11-28 13:57:58
So I'm attempting to build a semi complication Search expression, but I'm stuck trying to create a basic one. The expressions being used for getValueExpression look something like: x => x.PropertyA != null ? x.PropertyA.ToShortDateString() : "" //nullable datetime x => x.PropertyB //string property x => x.PropertyC != null x.PropertyC.ToString() : "" //nullable int Here is my function code, it currently errors when getValueExpression being of type Func that can't be compared to a string, which makes perfect sense and I understand why that is, but I'm having trouble figuring out how to make an

Combining expression trees

痞子三分冷 提交于 2019-11-28 13:02:01
I have the following expression: public Expression<Func<T, bool>> UserAccessCheckExpression<T>(int userId) where T : class { return x => (IsAdmin || userId == CurrentUserId || userId == 0); } Then I want to apply this filter to several collections (IQueryable) like this one: return tasks .Where(t => t.TaskUsers .Any(x => UserAccessCheckExpression<TaskUser>(x.User) && x.SomeBool == true)); I'm getting the following error while doing so: Error 40 Cannot implicitly convert type System.Linq.Expressions.Expression<System.Func<TaskUser,bool>> to bool I can't use workaround with interface inheritance

how can I convert IQueryable<string> to string?

只愿长相守 提交于 2019-11-28 10:56:48
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? 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 Comp.Name).Single(); There are four LINQ methods to retrieve a single item out of a sequence: Single()

.NET Entity Framework - IEnumerable VS. IQueryable

纵饮孤独 提交于 2019-11-28 10:44:35
Please see this line of code. This is an invocation of a stored procedure, which returns an ObjectResult<long?> . In order to extract the long values I added the Select: dbContext.FindCoursesWithKeywords(keywords).Select(l => l.Value); Based on intellisense this Select returns IEnumerable<long> . I'm not sure whether I read it somewhere or maybe just got used to this assumption - I always thought that when the EF API returns an IEnumerable (and not IQueryable ) then this means that the results have been materialized. Meaning they've been pulled from the database. I found out today that I was

IQueryable vs IQueryable<T>

谁都会走 提交于 2019-11-28 04:00:26
问题 I'm simply wondering why there is a IQueryable<T> version without the generic capability ? 回答1: 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. 回答2: I imagine it's the same reason as Jon Skeet