Entity Framework (QueryBuilder) - where the prefix "it"

I recently came across the following Entity Framework syntax. Check out the where clause -

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

What does it. do it. ? And why is it instead of Contacts.LastName? Any detailed information would be helpful. Thanks in advance.

 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); } 
+4
source share
1 answer

Based on this article , β€œthis” means ObjectQuery as the default name for parameter references. Using the Name property of an ObjectQuery object, you can change it so that in your where clause it can be specified like any name you want:

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

Probably not quite what is shown above, but it gives a general idea.

+5
source

Source: https://habr.com/ru/post/1401207/


All Articles