“选择值" -LINQ/Entity Framework查询中的value关键字 [英] "SELECT VALUE" - value keyword in LINQ/Entity Framework query

查看:61
本文介绍了“选择值" -LINQ/Entity Framework查询中的value关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此语句中的关键字值"是什么意思,我将在何处了解更多信息?
如果我遗漏关键字值"会怎样?在下面的代码中,z是一个实体框架类.

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

上面的代码在一个函数中,该函数返回类型为z的变量.

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

推荐答案

实体SQL 语法. Value关键字仅允许指定一个值,并且不添加行包装器.

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

阅读有关ESQL中SELECT语句的文章

实体SQL支持SELECT子句的两个变体.首先 变体,行选择,由SELECT关键字标识,可以是 用于指定一个或多个应该投影的值. 由于在返回的值周围隐式添加了行包装器, 查询表达式的结果始终是多行.

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.

行选择中的每个查询表达式必须指定一个别名.如果不 指定了别名,Entity SQL尝试通过使用生成别名 别名生成规则.

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.

SELECT子句的另一个变体,值选择,由以下项标识 SELECT VALUE关键字.它只允许指定一个值, 并且不添加行包装器.

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.

因此,如果要从查询中实现z对象,则应使用SELECT VALUE语法(否则将出现异常:从MaterializedDataRecord强制转换为z类型无效).

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

没有VALUE关键字,您将获得一组行:

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

这篇关于“选择值" -LINQ/Entity Framework查询中的value关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆