我最近遇到了以下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);
}
答案 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));
可能不完全像上面所示,但它给出了一般的想法。