简体   繁体   中英

Explanation of the usage of it. while using ObjectSet<>.Where()

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 eg " 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM