What is this in the Entity Framework

Forgive me if this was asked before, but “this” does not appear in any of my searches. I have two Person and Employee database tables modeling a Table-per-Type (e.g. Employee is-a Person). In my edmx designer, I defined a single Employee employee that maps each column to its corresponding base table (e.g. Name → Person, Salary → Employee).

"he" allows me to do such things in a LINQ expression:

context.Employees.Where("it.Name LIKE 'M%' AND it.Salary > 1234") 

Are there any good links explaining how I can expect this to behave? I assume that this is not a common LINQ thing and that it is somewhat specific to the Entity Framework.

EDIT 0: The generated C # code for the ObjectContext follows:

 public partial class TestObjectContext : ObjectContext { // lots of boilerplate removed for clarity public ObjectSet<Employee> Employees { get { if ((_Employees == null)) { _Employees = base.CreateObjectSet<Employee>("Employees"); } return _Employees; } } } 
+4
source share
2 answers

it is the default alias for the current ObjectQuery command. Please refer to the documentation for Query Builder methods, especially in the Alias ​​section:

Query construction methods are applied sequentially to create a cumulative query command. This means that the current ObjectQuery command is processed as a helper query to which the current method applies.

In the query builder method, you reference the current ObjectQuery command using an alias. By default, the string "it" is an alias that represents the current command, as in the following example:

 int cost = 10; // Return Product objects with a standard cost // above 10 dollars. ObjectQuery<Product> productQuery = context.Products .Where("it.StandardCost > @cost", new ObjectParameter("cost", cost)); 

When you set the Name property of an ObjectQuery, this value becomes an alias in subsequent methods. The following example extends the previous one by setting the ObjectQuery name to “product” and then using this alias in the following OrderBy method:

 // Set the Name property for the query and then // use that name as the alias in the subsequent // OrderBy method. productQuery.Name = "product"; ObjectQuery<Product> filteredProduct = productQuery.OrderBy("product.ProductID"); 
+7
source

You are talking about the Dynamic LINQ library.

If you have VS 2008 installed, you can find the detailed API document in

 <path to vs2008>\Samples\1033\CSharpSamples\LinqSamples\DynamicQuery\Dynamic Expressions.html 

Or download here

Here 's a Scott Gug article on dynamic LINQ

Here is an excerpt from the Dynamic Expressions.html file.

Current instance

When analyzing a lambda expression with one single parameter, the members of an unnamed parameter automatically enter the scope in the expression string, and the current instance specified by the unnamed parameter can generally refer to the it keyword. For instance,

 customers.Where("Country = @0", country); 

equivalently

 customers.Where("it.Country = @0", country); 

The IQueryable extension methods all parse their expression arguments as lambda expressions with a single unnamed parameter.

Dynamic lambda call

An expression can refer to other dynamic lambda expressions through dynamic lambda calls. A dynamic lambda call consists of a lookup variable identifier that references an instance of System.Linq.Expressions.LambdaExpression, followed by a list of arguments. The arguments given should be compatible with the parameter list of this dynamic lambda expression.

Next, we analyze two separate dynamic lambda expressions, and then combine them into a predicate expression using dynamic lambda calls:

 Expression<Func<Customer, bool>> e1 = DynamicExpression.ParseLambda<Customer, bool>("City = \"London\""); Expression<Func<Customer, bool>> e2 = DynamicExpression.ParseLambda<Customer, bool>("Orders.Count >= 10"); IQueryable<Customer> query = db.Customers.Where("@0(it) and @1(it)", e1, e2); 

Of course, you can combine static and dynamic lambda expressions this way:

 Expression<Func<Customer, bool>> e1 = c => c.City == "London"; Expression<Func<Customer, bool>> e2 = DynamicExpression.ParseLambda<Customer, bool>("Orders.Count >= 10"); IQueryable<Customer> query = db.Customers.Where("@0(it) and @1(it)", e1, e2); 

The above examples have the same effect as:

 IQueryable<Customer> query = db.Customers.Where(c => c.City == "London" && c.Orders.Count >= 10); 
+1
source

All Articles