Using entity framework, how do you select the datalength of a column plus other column data

给你一囗甜甜゛ 提交于 2019-12-24 14:00:59

问题


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

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

With the error:

error CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.


回答1:


Try explicitly specifying the anonymous type's member names:

var results = dc.X.Select(x => 
    new { Length = SqlFunctions.DataLength(x.B), A = x.A });


来源:https://stackoverflow.com/questions/15106616/using-entity-framework-how-do-you-select-the-datalength-of-a-column-plus-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!