Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"SELECT VALUE" - value keyword in LINQ/Entity Framework query

What does the keyword "value" mean in this statement, and where would I go to learn more?
What happens if I leave out the keyword "value"? In the code below, z is an entity framework class.

string queryString = "SELECT VALUE q from x.zs as q where q.a = @parm;"
ObjectQuery<z> query = context.CreateQuery<z> 
    (queryString, new ObjectParameter("parmname",parmvalue)); 
return query.First(); 

(This is a part of a practice question for an exam).

The above code is in a function that returns a variable of type z.

like image 891
NealWalters Avatar asked Mar 13 '13 13:03

NealWalters


People also ask

What does the SELECT keyword specify in a LINQ query?

select - In a LINQ query, specifies the objects or projections that will be created by the query. orderby - In a LINQ query, specifies one or more properties to order the results by. descending - In a LINQ query, specifies that the objects are to be ordered by the given property in descending order.

Can we use LINQ with Entity Framework?

The DbSet class is derived from IQuerayable . So, we can use LINQ for querying against DbSet , which will be converted to an SQL query. EF API executes this SQL query to the underlying database, gets the flat result set, converts it into appropriate entity objects and returns it as a query result.

What is include in LINQ query C#?

LINQ Include allows retrieving the related entities to be read from database in same query. By using the Include method we can easily read all related entities from the database in a single query.

What is the difference between LINQ to SQL and Entity Framework?

LINQ to SQL allow you to query and modify SQL Server database by using LINQ syntax. Entity framework is a great ORM shipped by Microsoft which allow you to query and modify RDBMS like SQL Server, Oracle, DB2 and MySQL etc. by using LINQ syntax. Today, EF is widely used by each and every .


1 Answers

That is Entity SQL syntax. Value keyword allows only one value to be specified, and does not add a row wrapper.

Read article about SELECT statement in ESQL

Entity SQL supports two variants of the SELECT clause. The first variant, row select, is identified by the SELECT keyword, and can be used to specify one or more values that should be projected out. Because a row wrapper is implicitly added around the values returned, the result of the query expression is always a multiset of rows.

Each query expression in a row select must specify an alias. If no alias is specified,Entity SQL attempts to generate an alias by using the alias generation rules.

The other variant of the SELECT clause, value select, is identified by the SELECT VALUE keyword. It allows only one value to be specified, and does not add a row wrapper.

So, if you want to materialize z object from your query, you should use SELECT VALUE syntax (otherwise you will get exception: cast from MaterializedDataRecord to z type is not valid).

Without VALUE keyword you will get set of rows:

string esql = "SELECT q from x.zs as q where q.a = @parm;";
ObjectQuery<DbDataRecord> query = context
       .CreateQuery<DbDataRecord>(esql, new ObjectParameter("parm",parmvalue)); 
var result = query.First();
like image 126
Sergey Berezovskiy Avatar answered Oct 20 '22 09:10

Sergey Berezovskiy