1

I have the following piece of code which is working fine:

ObjectContext octx = new ObjectContext("name=PublisherModelContainer");
        ObjectSet<Author> authorSet = octx.CreateObjectSet<Author>();
        ObjectQuery<Author> q = authorSet.Where("it.FirstName == @FirstName", new ObjectParameter("FirstName", "Isaak"));
        Author a = q.FirstOrDefault();
        if (a == null) 
        { 
            Console.WriteLine("Author not found");
            return;
        }
        Console.WriteLine("{0} {1}", a.FirstName, a.LastName);

While calling the 'Where' method, the FirstName property is being referenced via "it.FirstName". What does this mean? I have tried using a different alias e.g. "a.FirstName" but that fails with exception message 'a.FirstName' could not be resolved in the current scope or context.

Even in Microsoft's example here (https://msdn.microsoft.com/en-us/library/bb338811%28v=vs.110%29.aspx), it.ProductID is being used not something like t.ProductID.

What exactly is "it"? Is it that "it" has a special meaning?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Siddharth B
  • 344
  • 3
  • 11

1 Answers1

0

It is just how you can refer to the set you're currently working with. Same as with this in C# class. But this is achieved based on the query that EF generates. Consider the following SQL script.

SELECT Col1, Col2, Col3 FROM Table AS It
WHERE It.Col1 = @param1

Here is a link with more detailed explanation http://www.w3schools.com/sql/sql_alias.asp

Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
  • Thanks a lot. I find it a bit surprising that I didn't find 'it' mentioned specifically in any EF documentation. 'this' is a pretty well known language keyword mentioned in every C++ book or for instance here (http://en.cppreference.com/w/cpp/keyword), but 'it'..? – Siddharth B Mar 17 '15 at 04:23
  • There is no such keyword in SQL they (EF) just try to achieve the same concept by using table alias. This concept is only valid in the EF world pay close attention to what will the generated query be like :). Put yourself in their shoes and think about it if you have to generate the SELECT part of the query how would you expose what's available so that the user of your lib can supply the WHERE part? – Mihail Shishkov Mar 17 '15 at 08:53
  • That is understandable, its just that I would have expected the usage of "it" along with the meaning of "it" to be documented clearly somewhere. As yet I have not found any. Since it looks like an alias, developers would tend to replace with any name of their liking just as they do in SQL but this does not work, one must use "it". – Siddharth B Mar 18 '15 at 14:04