views:

269

answers:

2

Hi!

I'm just starting developing with ADO.NET Entity Framework and I don't know how I would write this:


ClientEntities clientContext = new ClientEntities();

   ObjectQuery clientQuery = 
        clientContext.Client.Where("it.email = @email AND it.password = @password",
                    new ObjectParameter("email", "[email protected]"),
                    new ObjectParameter("password", "xAdxar12s"));

            Console.WriteLine(clientQuery.Count());
            Console.ReadLine();

And it works!

But, what's this?


"it.email = @email AND it.password = @password"

Where can I learn this "SQL syntax"? I don't know why I have to use it.email or the meaning of SELECT VALUE. It is "Entity SQL"?

Thanks!

A: 

Where did you get that sample from? the normal way of doing this would be:

string email = "[email protected]",
       pw = "xAdxar12s";
var qry = from client in clientContext.Client
          where client.email == email
             && client.password == pw
          select client;

Console.WriteLine(qry.Count());

That gives you static typing, etc - and is just friendlier!

(note: check you have using System.Linq; at the top of the file)

Marc Gravell
I take follow this example: http://msdn.microsoft.com/en-us/library/cc716755.aspx#_QueryBuilderMaybe the way that you have proposed its friendlier for you but I following this tutorial: http://msdn.microsoft.com/en-us/library/bb399182.aspx
VansFannel
No, I don't using System.Linq
VansFannel
+1  A: 

Yes, this is Entity SQL.

"LINQ to Entities" is one way of retrieving entity instances from your context. Entity SQL is another. Both work, are supported, and complement each other.

SELECT VALUE means "this statement should return entity instances." (As opposed to a row wrapper.) You'll get an object query back.

Now, what is "it"? Well, Client is a property of the generated ObjectContext, of type ObjectQuery<Client>. You can look at the source code for Client in the codegen file for the model (the file with the extension .Designer.cs):

    /// <summary>
    /// There are no comments for Client in the schema.
    /// </summary>
    public global::System.Data.Objects.ObjectQuery<Client> Client
    {
        get
        {
            if ((this._Client== null))
            {
                this._Client = base.CreateQuery<Client>("[Client]");
            }
            return this._Client;
        }
    }
    private global::System.Data.Objects.ObjectQuery<Client> _Client;

Do you see the Entity SQL there? It's easy to miss if you don't look carefully. I'll copy it down here:

"[Client]"

I can't find the documentation for this syntax, but that's mostly because searching for square brackets and NOT any other Entity SQL keyword is sort of difficult to do with most search engines. Like T-SQL, the square brackets appear to mean "consider everything in between these as an identifier." But the statement appears to be shorthand for:

 "SELECT VALUE it FROM MyEntities.[Client] AS it"

So now you know where "it" comes from.

Craig Stuntz
Where can I learn more about Entity SQL?
VansFannel
Here: http://msdn.microsoft.com/en-us/library/bb399560.aspx
Craig Stuntz