使用实体框架,如何选择列的数据长度以及其他列数据 [英] Using entity framework, how do you select the datalength of a column plus other column data
问题描述
如果我有一个带有A和B列的SQL表X,并且我想选择B的DATALENGTH以及A列,那么如何在单个表达式中执行此操作?例如:
If I have a SQL table X with columns A and B, and I want to select the DATALENGTH of B, as well as column A, how do I do this in a single expression? For example:
var results = dc.X.Select(x => SqlFunctions.DataLength(x.B))
将为我返回包含等于B长度的单列的结果.如果我想在同一结果集中包含A,则此语句看起来像什么?我已经尝试过了,但是显然不会编译:
will return me results containing a single column equal to B's length. What does this statement look like if I want to include A in the same result set? I've tried this, but it won't compile obviously:
var results = dc.X.Select(x => new { SqlFunctions.DataLength(x.B), x.A });
出现错误:
错误CS0746:无效的匿名类型成员声明符.必须使用成员分配,简单名称或成员访问权限来声明匿名类型成员.
error CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
推荐答案
尝试显式指定匿名类型的成员名称:
Try explicitly specifying the anonymous type's member names:
var results = dc.X.Select(x =>
new { Length = SqlFunctions.DataLength(x.B), A = x.A });
这篇关于使用实体框架,如何选择列的数据长度以及其他列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!