I'm using EF 5.0 Code First API, and I need to add the ability for building dynamic OrderBy expressions (the UI has a filter panel where users can choose multiple oredering criteria (e. g. select top 20 Customers, order by LastName ASC, then by Birst Date DESC).
ObjectContext API exposes OrderBy("it.PropertyName") method which looks great, despite it's missing compile time verification. However, I can't find any analogues in the DbContext API.
Googling the problem I found such advice:
((IObjectContextAdapter)myContextInstance).ObjectContext.CreateQuery<MyEntity>("<ESQL Query>")
However, I would not like to give up LINQ query at all.
Are there any ways to have something like this (mix ESQL and LINQ, as it could be by using ObjectContext):
var customers = myDbContext.Customers.OrderBy("it.LastName desc").Where(c => c.Age < 18)
Something like this? I'm not sure about mixing ESQL and LINQ, but your last query can be done entirely from LINQ.
var customers = myDbContext.Customers.OrderByDescending(a => a.LastName).ThenBy(c => c.BirthDate).Where(b => b.Age < 18);
Related
How can I achieve following query method in Entity Framework,
below is a snippet from NHibernate documentation http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html
Example example = Example.create(cat)
.excludeZeroes() //exclude zero valued properties
.excludeProperty("color") //exclude the property named "color"
.ignoreCase() //perform case insensitive string comparisons
.enableLike(); //use like for string comparisons
List results = session.createCriteria(Cat.class)
.add(example)
.list();
Entity framework is LINQ based. Linq is said to be a declarative language, which means so much as telling what to do in stead of how to do it (imperative). A statement like
context.Orders.Select(o => o.OrderDate).Distinct();
is a declarative shortcut, if you like, for a 'ceremonial' foreach statement in which OrderDates are added to a list if they were not added to it before.
I'm not an expert in NHibernate or its criteria API, but the criteria API seems to be even more declarative than linq. That makes it hard to compare them. A few differences:
The main one: query by example is not possible in EF.
There is no way in linq to set behaviours for a whole query. For instance, if you want to exclude zero valued properties, you'll have to specify each one of them in a where predicate (which is closer to telling how to filter).
Case sensitivity is downright underdeveloped in EF. For example, a statement like
People.Where(c => string.Compare( c.Name, "z", false) > 0)
will generate the same SQL as
People.Where(c => string.Compare( c.Name, "z", true) > 0)
The database collation determines the case sensitiveness of string comparisons.
You can do LIKE queries, but, again, specified for each individual predicate:
People.Where (c => c.Name.Contains("a"))
(again: no differentiation in case)
So I can't really give a linq translation of your criteria query. I'd have to know the class properties to be able to specify all individual predicates.
I have DbContext (called "MyContext") with about 100 DbSets within it.
Among the domain classes, I have a Document class with 10 direct subclasses (like PurchaseOrder, RequestForQuotation etc).
The heirarchy is mapped with a TPT strategy.
That is, in my database, there is a Document table, with other tables like PurchaseOrder, RequestForQuotation for the subclasses.
When I do a query like:
Document document = myContext.Documents.First();
the query took 5 seconds, no matter whether it's the first time I run it or subsequently.
A query like:
Document document = myContext.Documents.Where(o => o.ID == 2);
also took as long.
Is this an issue with EF4.1 (if so, will EF4.2 help) or is this an issue with the query codes?
Did you try using SQL Profile to see what is actually sent to the DB? It could be that you have too many joins on your Document that are not set to lazy load, and so the query has to do all the joins in one go, bringing back too many columns. Try to send a simple query with just one return column.
As you can read here, there are some performance issues regarding TPT in EF.
The EF Team annouced several fixes in the June 2011 CTP, including TPT queries optimization, but they are not included in EF 4.2, as you can read in the comments to this answer.
In the worst case, these fixes will only be released with .NET 4.5. I'm hoping it will be sooner...
I'm not certain that the DbSet exposed by code-first actually using ObjectQuery but you can try to invoke the .ToTraceString() method on them to see what SQL is generated, like so:
var query = myContext.Documents.Where(o => o.ID == 2);
Debug.WriteLine(query.ToTraceString());
Once you get the SQL you can determine whether it's the query or EF which is causing the delay. Depending on the complexity of your base class the query might include a lot of additional columns, which could be avoided using projection. With using projections, you can perform a query like this:
var query = from d in myContext.Documents
where d.ID == 2
select new
{
o.Id
};
This should basically perform a SELECT ID FROM Documents WHERE ID = 2 query and you can measure how long this takes to gain further information. Of course the projected query might not fit your needs but it might get you on the right track. If this still takes up to 5 seconds you should look into performance problems with the database itself rather than EF.
Update
Apparently with code-first you can use .ToString() instead of .ToTraceString(), thanks Slauma for noticing.
I've just had a 5 sec delay in ExecuteFunction, on a stored procedure that runs instantaneously when called from SQL Management Studio. I fixed it by re-writing the procedure.
It appears that EF (and SSRS BTW) tries to do something like a "prepare" on the stored proc and for some (usually complex) procs that can take a very long time.
A quick and dirty solution is to duplicate and then replace your SP parameters with internal variables:
create proc ListOrders(#CountryID int = 3, #MaxOrderCount int = 20)
as
declare #CountryID1 int, #MaxOrderCount1 int
set #CountryID1 = #CountryID
set #MaxOrderCount1 = #MaxOrderCount
select top (#MaxOrderCount1) *
from Orders
where CountryID = #CountryID1
I would like to hear some feedback on a scenario that I am trying to implement. I have currently implemented this scenario using Criteria API for NHibernate and was wondering if there was anything similar that was implemented for Entity Framework 4.1.
I have a need where the end user can select filtering criteria from the UI and thus build a query that can contain complex AND/OR criteria.
For e.g.: The user can say: I want Students with (Zip Code = 92037 AND Gender = F) OR (ZipCode = 92101 and Gender = M)
OR
I want students with (State = CA OR State = FL) AND GPA = 4.0 AND GENDER = M
These queries are usually built using a tree control on the front end.
I currently have this working using NHibernate. The Criteria API in NHibernate is really awesome for doing this. However, NHibernate has a major bug, and that is it doesn't allow multiple joins on a 1:many table.
So for e.g. if I had a table containing a CATCODE (Category code) and an Answer, NHibernate will not currently let me do multiple querying using Criteria API.
So I cannot for e.g. do: WHERE CATCODE = A and Answer in (A,B,C) AND CATCODE = B and Answer in (V,H,Y).
Due to this limitation, I have been trying to move out of NHibernate into Entity Framework. I didn't know if there was a nice way to do this kind of stuff using APIs.
Can anyone tell me a better solution to achieve such functionality, if there is one?
Would love to hear from both NHibernate and EF experts if there are ways to solve this.
I don't really know NHibernate or any other OR/M except the entity framework but let assume you have an entity called Category, and category have relation to Answers (each category has many answers for example).
So the Category entity looks like:
public class Category {
public virtual string CATCODE{get;set}
public virtual IList<Answer> Answers{get;set;}
}
And then you can do something like:
Categories.Where(x=>x.CATCODE =="A").Where(x=>x.Answers.Any(l=>l.Name=="1" || l.Name=="2" || l.Name==3)
I even sure there is a better way to write this query, but the point is that it is possible.
I found several times people asking for the same question but it seems that the answer was never satisfying altough it should be pretty easy (in theory). Here is my question :
I have an entity called "Company" inside which I have an entityCollection "Employees" (one to many). I need to retrieve all Companies and for each of them, I only want the employees with an Age greater than 21.
I tried :
Return context.Companies.Include("Employees").Where(c => c.Employees.Where(e => e.Age > 21).Count() > 0)
That doesn't work as it gives me all employees for each company if there is at least one above 21 (it is actually the same than .Any() )
I tried :
Return context.Companies.Include("Employees").Select(c => New Company {
.Id = c.Id,
.Employees = c.Employees.Where(Function(e) e.Age > 24)
}).ToList()
That didn't work either (although it would have been perfect), it gives me the following error : The entity or complex type 'MyModel.Company' cannot be constructed in a LINQ to Entities query.
How can you select all my companies with only, for each of them, the employees being above 21 ? At the moment, I select all and on the client side, I filter my employees but I don't like that solution.
Can anybody help me ?
Thank you Morteza Manavi-Parast, it will do the work !
Nevertheless, I hardly convince myself that doing so in a unique query has not be implemented in the Entity framework. It is such a relatively common situation ... As a prove, there are numbers of questions like mine on this forum.
I am surprised ... Maybe for the next release ?
To be clear, I need a list of Companies as I am directly binding the result of my query to a datagrid. For your information, when I click on a row of my datagrid (so selecting a company), I have a second Grid which is populated with its employees (above 21 years old) coming from the entityCollection.
There is no way to have a "Conditional Eager Loading" with include in LINQ to Entities. There are 2 workarounds exist though. The first one is Filtered Projection and it's the one that Justin suggested but might not be desirable in all situations as it gives a collection of anonymous type objects.
The second way is called Two Tracked Queries which gives you a collection of strongly types Companies whose their employees satisfy a condition and I believe that's what you are looking for. Here is the code for it:
var companies = context.Companies.ToList();
var employees = context.Employee.Where(e => e.Age > 21);
foreach (var employee in employees) {
companies.Single(c => c.CompanyID == employee.CompanyID).Employees.Add(employee);
}
Please take a look at Conditional Eager Loading for another example.
Instead of using the type Company, have you tried selecting a new anonymous type:
Return context.Companies.Include("Employees").Select(c => New With {
.Id = c.Id,
.Employees = c.Employees.Where(Function(e) e.Age > 24)
}).ToList()
(Sorry if the syntax is a little off, it's been a while since I've done LINQ/Anonymous Types in VB.NET)
You might be over-thinking this one. If you have the Company => Employee relationship bi-directionally mapped, then just do the select on Employee with the where clause and include company.
Return context.Employee.Include("Company").Where(e => e.Age > 21)
Greetings,
Considering the Northwind sample tables Customers, Orders, and OrderDetails I would like to eager load the related entities corresponding to the tables mentioned above and yet I need ot order the child entities on the database before fetching entities.
Basic case:
var someQueryable = from customer in northwindContext.Customers.Include("Orders.OrderDetails")
select customer;
but I also need to sort Orders and OrderDetails on the database side (before fetching those entities into memory) with respect to some random column on those tables. Is it possible without some projection, like it is in T-SQL? It doesn't matter whether the solution uses e-SQL or LINQ to Entities. I searched the web but I wasn't satisfied with the answers I found since they mainly involve projecting data to some anonymous type and then re-query that anonymous type to get the child entities in the order you like. Also using CreateSourceQuery() doesn't seem to be an option for me since I need to get the data as it is on the database side, with eager loading but just by ordering child entities. That is I want to do the "ORDER BY" before executing any query and then fetch the entities in the order I'd like. Thanks in advance for any guidance. As a personal note, please excuse the direct language since I am kinda pissed at Microsoft for releasing the EF in such an immature shape even compared to Linq to SQL (which they seem to be getting away slowly). I hope this EF thingie will get much better and without significant bugs in the release version of .NET FX 4.0.
Actually I have Tip that addresses exactly this issue.
Sorting of related entities is not 'supported', but using the projection approach Craig shows AND relying on something called 'Relationship Fixup' you can get something very similar working:
If you do this:
var projection = from c in ctx.Customers
select new {
Customer = c,
Orders = c.Orders.OrderByDescending(
o => o.OrderDate
)
};
foreach(var anon in projection )
{
anon.Orders //is sorted (because of the projection)
anon.Customer.Orders // is sorted too! because of relationship fixup
}
Which means if you do this:
var customers = projection.AsEnumerable().Select(x => x.Customer);
you will have customers that have sorted orders!
See the tip for more info.
Hope this helps
Alex
You are confusing two different problems. The first is how to materialize entities in the database, the second is how to retrieve an ordered list. The EntityCollection type is not an ordered list. In your example, customer.Orders is an EntityCollection.
On the other hand, if you want to get a list in a particular order, you can certainly do that; it just can't be in a property of type EntityCollection. For example:
from c in northwindContext.Customers
orderby c.SomeField
select new {
Name = c.Name,
Orders = from o in c.Orders
orderby c.SomeField
select new {
SomeField = c.SomeField
}
}
Note that there is no call to Include. Because I am projecting, it is unnecessary.
The Entity Framework may not work in the way you expect, coming from a LINQ to SQL background, but it does work. Be careful about condemning it before you understand it; deciding that it doesn't work will prevent you from learning how it does work.
Thank you both. I understand that I can use projection to achieve what I wanted but I thought there might be an easy way to do it since in T-SQL world it's perfectly possible with a few nested queries (or joins) and order bys. On the other hand seperation of concerns sounds reasonable and we are in the entity domain now so I will use the way you two both recommended though I have to admit this is easier and cleaner to achieve in LINQ to SQL by using AssociateWith.
Kind regards.