2012-03-13 65 views
4

我最近遇到下面的实体框架语法。注意where子句 -实体框架(QueryBuilder) - 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

我从来没有必要在'where'前面添加我的where子句。...... – c0deNinja 2012-03-13 17:11:28

+0

什么版本的EF? – c0deNinja 2012-03-13 17:12:32

+0

EF 4 - 这里是MSDN文章 - http://msdn.microsoft.com/en-us/library/bb896238.aspx – bugBurger 2012-03-13 19:49:10

回答

5

基于this article,“它”指的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)); 

也许不完全像上面显示,但它给出了总体思路。

+0

感谢您的好解释和参考文章。 – bugBurger 2012-03-13 18:06:43

+0

当我看到这个问题时,我就像我知道我以前见过的那样,但我并没有真正明白它的意思。你的问题让我有理由更好地理解它。 – ShelbyZ 2012-03-13 18:49:15