I have some big query with 8 joins and I want to go over each entity attributes in the query expression result for duplicate those entities.
I've tried to split the result attributes but I'm not sure how to do it.
Thanks
If I understand your question correctly, you may be looking for the OfType linq method?
Related
Is it possible to use Xpath or Xquery to query XML type fields in a database using Eloquent ORM ? I find in inconvenient to parse the XML every time and filter my results in memory. I would rather return my results from a query, is there a way to achieve that using Eloquent or I'm stuck with raw database statements?
Thank you
I reviewed the options for selecting items in Eloquent and there is not much good options to select based on XML values as you expect. You can use some wildcards and run calculations on Eloquent to filter needed data but none of these are very efficient.
I have the following query:
var query = db.Prog
.Where (a => a.Prog != "00000" && a.fn != "Koll")
.Select(a => new {a.Prog, a.MEfn})
.OrderByDescending(a => a.MEfn)
The query works fine but wondering if there are general rules on the order in which you write a Lambda linq query. Meaning, .Where comes before .Select, etc.
Can somebody enlighten me on the order in which LINQ needs to be written or best practices.
There isn't a best practice on the order in which you write a LINQ query, it will depend on if you want to do your filtering first, or your projection. For example in your case, you are projecting to an anonymous type which doesn't include the 'fn' property which your filter uses, so it wouldn't be available to use in a where clause if your select was first.
A better practice would be to give your properties less cryptic names. Also, 'fn' doesn't follow the PascalCase for property names, and if it's a field then it probably shouldn't be public.
Yours can be a good order.
Let's distinguish the case where db points to an SQL DB with a very good LINQ provider and the case db is an in-memory object. I guess it's the first.
In case you are using a LINQ to SQL provider, the statements are evaluated only when you materialize the query into an object, so the SQL optimizer (inside the DB) will take care of ordering of statements.
The vice versa occurs when your statements are run against in-memory collections or to materialized collections coming from LINQ to SQL. In that case they are executed sequentially, so you want to execute first those statements that reduce the number of results in the collection. Where is the best candidate!!!
The order that they should be in are completely dependent on the context of what you are doing. So if your OrderBy is simply formatting the data to be friendly to view, put it at the end after you have trimmed your collection, if your looking for the First value of a sorted collection then maybe you would need it before the collection is iterated to get the first.
I Just want to make sure I understand this correctly...
search is an object that contains a querystring.
Repo.Query returns an ObjectQuery<T>.
From my understanding the chained linq statements will filter the results after entity framework has returned all the rows satisfying the query. So really ALL the rows are being returned and THEN filtered in memory. So we are returning a bunch of data that we don't really want. There's about 10k rows being returned so this is kind of important. Just like to get my confusion cleared up.
var searchQuery = Repo.Query(search)
.Where(entity =>
entity.Prop1.ToUpper().Equals(prop1.ToUpper()) &&
entity.Prop2.ToUpper().Equals(prop2.ToUpper()))
.OrderBy(entity => Repo.SortExpression ?? entity.prop1);
Your Repo.Query(string query) function should return IQueryable<T>.
Then you can filter and order without getting all rows first.
IQueryable(Of T) Interface
hope this helps
If this is to SQL, this will most likely create a SQL query and filter on the server and not in memory.
As a matter of fact, the statement above wouldn't actually do anything.
It's only when you iterate over it that the query will be executed. This is why certain providers (like the EF to SQL one) can collapse expression trees into a SQL query.
Easiest way to check is to use LINQPAD or the SQL Profiler to see what query is actually is executed.
I am creating a method that can create filter understood by NHibernate (by filter i mean a set of ICriteria object for example) from my abstract filter object.
public static IEnumerable<ICriterion> ToNhCriteria(this MyCriteria criteria)
{
// T4 generated function
// lots of result.Add(Expression.Or(Expression.Eq(),Expression.Eq)) expression trees - hard to generate
// Is there a way to generate HQL/Linq query here istead?
}
then i want to do something like
session.CreateCriteria<Entity>().Add(myCriteria.ToNhCriteria())
to filter entities.
The problem is that using Expression. methods (Expression.Or etc) is quite tedious (the method is generated and i have multiple or statements that have to be joined into an expression somehow).
Is there a way to avoid using Expression.Or() and create ICrietrion / ICriteria using LINQ or HQL?
Hey, did you check out this question? It shows going from Linq to NHibernate to a MultiCriteria (and on the way transforms a linq query to an ICriteria)
No that is not possible. Why don't you use linq instead of criteria?
Linq is not the best solution unless you want to do filtering on collection-side not on datbase-side using WHERE clauses.
Ayende suggests that ICriteria API is well suited for dynamic filter creation, the problem i had with multiple ORs has been tackled by using Restrictions.Disjunction()... that simplified a lot
At the time I asked the question I just didn't realize such things exist in NHibernate :)
I thought that the purpose of using Linq2Nibernate was to return IQueryable and build up an expression tree. I am not sure if this is correct or not.
Why would anyone use Linq2Nibernate if they are not going to return IQueryable?
What else would it be used for?
I would love some more input on this topic
Linq For Nhibernate
I'm planning to use NHibernate.Linq to replace my HQL and criteria API queries with LINQ expressions. In other words, I'll generate the query in code (as a LINQ expression) and then pass it to NHibernate.Linq and NHib to convert it into a database query.
FYI there is an alpha version available.
I have planed to start using Linq2Nibernate but haven't got round to i yet.
My reason for wanting to user Linq2Nibernate is the nice syntax when constructing criterions and later querying them out.
Here is a nice simple example.
http://ayende.com/Blog/archive/2007/03/16/Linq-for-NHibernate.aspx
I am using Linq2Nhibernate with my repository pattern that returns IQueryable objects.
As you know, IQueryable is only a query definition - doesn't touch database yet.
Then local requirements are added to the query and finally the object or list is materialized.
Seems to work excellent and prevents unnecessary db queries for partial data at higher abstract layers.
What's Linq2NHibernate? As there are several projects which tried to implement a linq provider for nhibernate but all stopped before reaching completion.
Any linq provider needs to return IQueryable, or better an IEnumerable as that's how linq works. It's convenient to return an IQueryable as you then can re-use existing code to pad additional operators to an already created query (as ctx.Employee which might return IQueryable is already a query)