如果之前有人问过这个问题,请原谅我,但“它”没有出现在我的任何搜索中。我有两个数据库表 Person 和 Employee 为一个 Table-per-Type 建模(例如 Employee is-a Person)。在我的 edmx 设计器中,我定义了一个实体 Employee,它将每一列映射到它们各自的基础表(例如 Name -> Person,Salary -> Employee)。
“它”允许我在 LINQ 表达式中做这样的事情:
context.Employees.Where("it.Name LIKE 'M%' AND it.Salary > 1234")
是否有任何好的链接可以解释我如何期望“它”的行为?我认为它不是通用的 LINQ 东西,它在某种程度上特定于 Entity Framework。
编辑 0: 为 ObjectContext 生成的 C# 代码如下:
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;
}
}
}
最佳答案
它
是当前 ObjectQuery
命令的默认别名。请引用documentation对于查询生成器方法,尤其是别名部分:
查询构建器方法按顺序应用以构建累积查询命令。这意味着当前的 ObjectQuery 命令被视为应用了当前方法的子查询。
在查询生成器方法中,您可以使用别名引用当前的 ObjectQuery 命令。默认情况下,字符串“it”是表示当前命令的别名,如下例所示:
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));
当您设置 ObjectQuery 的 Name 属性时,该值将成为后续方法中的别名。以下示例通过将 ObjectQuery 的名称设置为“product”,然后在后续的 OrderBy 方法中使用此别名来扩展前一个示例:
// 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");
关于c# - Entity Framework 中的 "it"是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720664/