Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "it" in Entity Framework

Forgive me if this has been asked before but "it" doesn't come up in any of my searches. I have two database tables Person and Employee modeling a Table-per-Type (e.g. Employee is-a Person). In my edmx designer I have defined a single entity Employee that maps each column to their respective underlying table (e.g. Name -> Person, Salary -> Employee).

"it" allows me to do stuff like this 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 "it" to behave? I presume it's not a generic LINQ thing and that it is somewhat specific to 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;
    }
  }
}
like image 336
Jono Avatar asked Feb 23 '23 13:02

Jono


1 Answers

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

Query builder methods are applied sequentially to construct a cumulative query command. This means that the current ObjectQuery command is treated like a sub-query to which the current method is applied.

In a query builder method, you refer to the current ObjectQuery command by using an alias. By default, the string "it" is the 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, that value become the alias in subsequent methods. The following example extends the previous one by setting name of the ObjectQuery to "product" and then using this alias in the subsequent 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");
like image 78
BrokenGlass Avatar answered Mar 05 '23 08:03

BrokenGlass