实体框架(QueryBuilder) - where子句“it”前缀

时间:2012-03-13 17:02:17

标签: .net entity-framework where-clause

我最近遇到了以下Entity Framework的语法。注意where子句 -

.Where("it.LastName = @ln AND it.FirstName = @fn")

it.做什么?为什么it而不是Contacts.LastName?任何详细信息都会有所帮助。提前谢谢。

string firstName = @"Frances";
string lastName = @"Adams";

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Get the contacts with the specified name.
    ObjectQuery<Contact> contactQuery = context.Contacts
        .Where("it.LastName = @ln AND it.FirstName = @fn",
        new ObjectParameter("ln", lastName),
        new ObjectParameter("fn", firstName));

    // Iterate through the collection of Contact items.
    foreach (Contact result in contactQuery)
        Console.WriteLine("Last Name: {0}; First Name: {1}",
        result.LastName, result.FirstName);
}

1 个答案:

答案 0 :(得分:5)

基于this article,“it”将ObjectQuery称为引用参数的默认名称。通过使用ObjectQuery的Name属性,您可以实际更改它,以便在您的where子句中可以将其引用为您希望的任何名称:

ObjectQuery<Contact> contactQuery;
contactQuery.Name = "contact";
contactQuery = context.Contacts
  .Where("contact.LastName = @ln AND contact.FirstName = @fn",
   new ObjectParameter("ln", lastName),
   new ObjectParameter("fn", firstName));  

可能不完全像上面所示,但它给出了一般的想法。