iqueryable

How to invoke Expression<Func<Entity, bool>> against a collection

邮差的信 提交于 2019-12-06 19:06:22
问题 I have an interface that defines a repository from the Repository pattern: interface IRepository { List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression); } I've implemented it against Entity Framework: class EntityFrameworkRepository { public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression) { return DBContext.Customers.Where(expression).ToList(); } } That seems to work well, it allows me to do something like: var customers =

IQueryable vs IEnumerable: is IQueryable always better and faster?

大憨熊 提交于 2019-12-06 09:45:11
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() ? If the IQueryable perform the query Expression in the server rather than fetching all records like IEnumerable , why IQueryable not replaced by IEnumerable where it can be faster and more efficient?

Entity Framework - IQueryable to DataTable

谁都会走 提交于 2019-12-06 05:37:08
问题 I'm trying to convert an IQueryable object to a DataTable . Here's an example of a query that I would like to convert to a DataTable : var query = DbContext.SomeObjectSet.Select(x => new { PropertyName1 = x.ColumnName1, PropertyName2 = x.ColumnName2 }); Please note the anonymous object that is created in the Select method and the names of the properties: new { PropertyName1 = x.ColumnName1, PropertyName2 = x.ColumnName2 } After Googling this issue, I came across the following code that

Dynamic Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> Expression

随声附和 提交于 2019-12-06 02:02:33
问题 I am using patterns mentioned here http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application And i am using method below to query EF public virtual IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = dbSet; if (filter != null) { query = query.Where

How do you transfer the execution of a Expression created by an IQueryable object to a IEnumerable?

五迷三道 提交于 2019-12-06 00:29:21
In my code I'd like to make my repositories IQueryable. This way, the criteria for selection will be a linq expression tree. Now if I want to mock my repository in theorie this is very easy : just implement the interface of my repository (which is also a IQueryable object). My mock repository implementation would be only a in memory collection, but my question is : Do you know an easy way to implement the IQueryable interface of my mock, to transfer the query to my in-memory collection (IEnumerable) ? Thanks for your response, Some precision The client object of my repository will use my

How to return an IQueryable<Something> as an IQueryable<ISomething>

牧云@^-^@ 提交于 2019-12-05 17:26:51
I have a class Something that implements ISomething . How can I convert/cast from an IQueryable<Something> to an IQueryable<ISomething> . When I try to cast, I am able to compile, but the result of the cast is always NULL. Background: The reason I am doing this is because my Something class is a CodeSmith-generated class (PLINQO template) that has table mapping decoration, methods, etc. My ISomething interface is "POCO-style", in that it is dumb and simply mirrors the properties of the Something class. It also exists in it's own namespace. I am doing this so I don't have to reference the

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

北城以北 提交于 2019-12-05 16:46:22
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 Entity of which the Code == "baz" , the parent's Code == "bar" and the parent of the parent's Code ==

NHibernate Linq Query with Projection and Count error

一笑奈何 提交于 2019-12-05 16:14:01
I have the following query: var list = repositoy.Query<MyClass>.Select(domain => new MyDto() { Id = domain.Id, StringComma = string.Join(",", domain.MyList.Select(y => y.Name)) }); That works great: list.ToList(); But if I try to get the Count I got an exception: list.Count(); Exception NHibernate.Hql.Ast.ANTLR.QuerySyntaxException A recognition error occurred. [.Count[MyDto](.Select[MyClass,MyDto](NHibernate.Linq.NhQueryable`1[MyClass], Quote((domain, ) => (new MyDto()domain.Iddomain.Name.Join(p1, .Select[MyListClass,System.String](domain.MyList, (y, ) => (y.Name), ), ))), ), )] Any idea how

IQueryable order by two or more properties

老子叫甜甜 提交于 2019-12-05 08:56:22
问题 I am currently ordering a list of custom objects using the IQueryable OrderBy method as follows: mylist.AsQueryable().OrderBy("PropertyName"); Now I am looking to sort by more than one property. Is there any way to do that? Thanks, Yannis 回答1: OrderBy(i => i.PropertyName).ThenBy(i => i.AnotherProperty) In OrderBy and ThenBy you have to provide keySelector function, which chooses key for sorting from object. So if you know property name only at runtime then you can make such function with

Converting IQueryable<object> results to comma delimited string

谁说我不能喝 提交于 2019-12-05 08:41:55
I have a LINQ query that returns all absences for an employee. The first part of the linq statement gets a basic list of the employees details, but I also return an IQueryable list of illnesses related to that absence. I'd like to somehow convert that IQueryable list to a comma delimited list of Illnesses. Currently I use (majorly stripped down): DetailsOfSickness = ( from t2 in Illnesses join ai1 in AbsenceIllnesses on t2.IllnessID equals ai1.IllnessID select new { Illness = ", " + t2.IllnessName }) Which returns the list but I'd like the results like: Headache, Flu, Cramps.... etc. Any ideas