Is necessary to compile linq queries in subsonic? - linq

I'd like to know if it's necessary to compile linq queries to the subsonic entities?
For example would I'd need to compile the following linq query?
var comments = from a in All()
where a.ParentCommentId == ArticleCommentId
select a;

Compiled queries are an optional optimization that allow you avoid parsing an expression tree multiple times. It's never required, but may be necessary to meet your performance requirements.

Related

Compiled LINQ for dynamic query

Is it possible to use compiled LINQ in Dynamic LINQ query?
There is this function.
protected IQueryable<DbT> GetPage(TTMSDataContext db, IQueryable<DbT> query, int maximumRows, int startRowIndex)
{
return query.Skip(startRowIndex).Take(maximumRows);
}
Its been used by multiple linq query execution. Is it possible to apply complied LINQ in this function?.
No, you can't compile a dynamic query like that. You may be able to compile the queries that call that function, maybe.
Are you trying to compile it so that it executes faster, or is there another reason?

Linq to ... and Lambda Expressions

I have a big question that has been puzzling me for a long time and that I can't seem to get a straight answer for anywhere and I am sure that if someone can answer this with authority and with good examples that it will help not only me but thousands of developers to come. All I want to know is what are the characteristics of the following concepts and what are the differences between them
Linq
Linq to SQL
Linq to Entities
Linq to Objects
Lambda expressions
Also, in particular, can someone tell us where constructs such as these fall into the above categories
Construct 1
var result = from n in nums
where n < 5
orderby n
select n;
Construct 2
Entities.Person.Where(p => p.FirstName == "John").First();
Your learned clarifications are eagerly awaited.
I'm sure there are some over-simplifications here, but for what it's worth:
Linq is an API designed for dealing with data sets. IQueryable, for instance, comes from the System.Linq namespace. Linq to ... are different implementations that parse the same Linq instructions to perform different operations based on how your data is stored. So Linq to SQL will parse your .Where instruction to produce a sql query with a WHERE clause, while Linq to Objects would take that same instruction and produce a foreach.
A lambda expression is pretty much a shorthand for an anonymous delegate. You identify a lambda by the => operator. Your argument list is at the left hand of the => and the expression that can access those arguments and optionally return a result is at the right hand.
Your two code examples are different syntaxes in which Linq queries can be written. They are called Query syntax and Method syntax, respectively.

Constructing dynamic linq queries based on user input

I am using dynamic linq on a project but don’t know how to run a particular query based on parameters. The example below shows the dynamic linq used to run 3 separate queries where a user has entered a filter expression or filer and sort expression etc. Without using if statements to check I (which seems incorrect and messy as I could end up with lots of if statements to cover the many different permutations ) I am unsure of how to generate the statements dynamically. Is this possible? I am running this query against Entity Framework.
context.Users.AsQueryable().Where(filterExpression)
context.Users.AsQueryable().Where(filterExpression).OrderBy(sortExpression)
context.Users.AsQueryable()Take(10).Where(filterExpression).OrderBy(sortExpression)
That should be achieved using expression tree only or may be using if..else clause
How to: Use Expression Trees to Build Dynamic Queries (C# and Visual Basic)

Dynamic linq: passing entire query as string

Looking at Dynamic Linq, it's possible to use strings to define the key parts of the query. My question is, is it possible to pass the entire query in as a string?
ie: var foo = "from..."
LINQ stands for "Language INtegrated Queries" - it's compiled with the rest of the code, not parsed on the run. You can use Microsoft.CSharp.CSharpCodeProvider to compile your query on the run - but you will have to know in advance what local objects you want to send to the query.
That's not possible in dynamic LINQ -- dynamic LINQ only replaces specific pieces of the query. You can use ExecuteQuery on the data context in LINQ to SQL or SqlQuery on a DbSet<T>, though, to execute specific SQL commands.

Is it possible to create ICriteria/ICriterion from LINQ or HQL?

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 :)

Resources