linq - "SELECT VALUE"- LINQ/ Entity Framework 查询中的值关键字

标签 linq entity-framework linq-to-entities

此声明中的关键字“值”是什么意思,我可以去哪里了解更多信息?
如果我省略关键字“value”会发生什么?在下面的代码中,z 是一个 Entity Framework 类。

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

(这是考试练习题的一部分)。

上面的代码在一个返回 z 类型变量的函数中。

最佳答案

Entity SQL句法。 Value关键字只允许指定一个值,并且不添加行包装器。

阅读 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.



所以,如果你想实现 z您查询中的对象,您应该使用 SELECT VALUE语法(否则你会得到异常:从 MaterializedDataRecord 转换到 z 类型是无效的)。

VALUE关键字您将获得一组行:
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 - "SELECT VALUE"- LINQ/ Entity Framework 查询中的值关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15386766/

相关文章:

linq - 在 Linq 中旋转

sql-server - Linq 转 Sql : SQL Default Value overridden

c# - 使用 XDocument 按属性查找元素

c# - 如何优化此 LINQ to EF/Sql 查询(多对多关系)?

c# - Entity Framework 按孙实体过滤

linq - linq 中的 .edmx 和 .dbml 文件有什么区别?

c# - LINQ 对象 : How to join different properties in the same List of objects

c# - linq,选择列作为 IEnumerable<DataRow>

c# - 命名空间中不存在类型或命名空间名称 'Core'

entity-framework - Sql to LINQ 查询 LINQ 查询转换