什么是“它”在实体框架中 [英] What is "it" in Entity Framework

查看:101
本文介绍了什么是“它”在实体框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我,如果以前曾经提出过这个问题,但是在我的任何搜索中都没有提到它。我有两个数据库表Person和Employee建模一个Table-per-Type(例如Employee is-a Person)。在我的edmx设计器中,我定义了一个单一的实体员工,将每个列映射到各自的底层表(例如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 
{
//为清晰起见删除了大量样板b
$ b public ObjectSet< Employee>员工
{
get
{
if((_Employees == null))
{
_Employees = base.CreateObjectSet< Employee>(Employees );
}
return _Employees;
}
}
}


解决方案

是当前 ObjectQuery 命令的默认别名。请参阅查询生成器方法的文档,特别是别名部分:



按顺序应用查询构建器方法以构建累积查询命令。这意味着当前的ObjectQuery命令被视为应用当前方法的子查询。



在查询构建器方法中,使用别名引用当前的ObjectQuery命令。默认情况下,字符串it是表示当前命令的别名,如以下示例所示:

  int cost = 10; 
//返回产品对象的标准成本
//高于10美元。
ObjectQuery< Product> productQuery =
context.Products
.Where(it.StandardCost> @cost,新的ObjectParameter(cost,cost));

当您设置ObjectQuery的Name属性时,该值将成为后续方法中的别名。以下示例通过将ObjectQuery的名称设置为product,然后在随后的OrderBy方法中使用此别名来扩展上一个示例:

  //设置查询的Name属性,然后
//使用该名称作为后续
// OrderBy方法中的别名。
productQuery.Name =product;
ObjectQuery< Product> filteredProduct = productQuery.OrderBy(product.ProductID);


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;
    }
  }
}

解决方案

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");

这篇关于什么是“它”在实体框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆