调用LINQ查询里面的方法 [英] Calling a method inside a Linq query
问题描述
我要插入我的表名为S栏,将获得基于它从表列获得一个值一些字符串值。
例如对于:每个ID(AZ)
我要得到它的存储在另一个表字符串值。该字符串的值是通过得到它通过LINQ查询另一种方法返回。
- 是否有可能调用从LINQ的方法?
- 我应该做在同一个查询的一切吗?
这是我需要的信息结构得到:
AZ是在表1的第一个方形的ID,从这个ID,我得到表2的另一个ID,从我可以得到我需要在列'S'。结果
块引用>
VAR q =(从单位VA JOIN b中艾VB的
等于BJ
其中AK ==AAA&放大器;&放;啊== 0
选择新{T = AI,S =的someMethod(AZ)的ToString()} )
返回q;
行
S =的someMethod(AZ)的ToString()
引起以下错误:
无法投类型的对象'System.Data.Linq.SqlClient.SqlColumn'
键入System.Data.Linq.SqlClient.SqlMethodCall。
块引用>
解决方案您在
LINQ到对象
的背景下,因为在数据库端的方法调用将没有任何意义,以执行方法的调用 - 你可以用做AsEnumerable()
- 基本上查询的其余部分将然后用评估为内存集合LINQ到对象
,你可以使用方法调用预期:VAR q =(从VA中加入对AI VB的
b等于BJ
,其中AK ==AAA&放大器;&放;啊== 0
选择新{T = AI,Z = AZ})
.AsEnumerable()
。选择(X = gt;新建{T = XT,S =的someMethod(XZ)的ToString()})
I want to insert into my table a column named 'S' that will get some string value based on a value it gets from a table column.
For example:
for each ID (a.z)
I want to gets it's string value stored in another table. The string value is returned from another method that gets it through a Linq query.
- Is it possible to call a method from Linq?
- Should I do everything in the same query?
This is the structure of the information I need to get:
a.z is the ID in the first square in table #1, from this ID I get another id in table #2, and from that I can get my string value that I need to display under column 'S'.
var q = (from a in v.A join b in v.B on a.i equals b.j where a.k == "aaa" && a.h == 0 select new {T = a.i, S = someMethod(a.z).ToString()}) return q;
The line
S = someMethod(a.z).ToString()
causing the following error:Unable to cast object of type 'System.Data.Linq.SqlClient.SqlColumn' to type 'System.Data.Linq.SqlClient.SqlMethodCall'.
解决方案You have to execute your method call in
Linq-to-Objects
context, because on the database side that method call will not make sense - you can do this usingAsEnumerable()
- basically the rest of the query will then be evaluated as an in memory collection usingLinq-to-Objects
and you can use method calls as expected:var q = (from a in v.A join b in v.B on a.i equals b.j where a.k == "aaa" && a.h == 0 select new {T = a.i, Z = a.z }) .AsEnumerable() .Select(x => new { T = x.T, S = someMethod(x.Z).ToString() })
这篇关于调用LINQ查询里面的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!