我想在我的表中插入一个名为“S”的列,该列将根据从表列中获取的值获取一些字符串值。
例如:对于每个 ID (a.z)
我想获取存储在另一个表中的字符串值。字符串值是从另一个通过 Linq 查询获取它的方法返回的。
- 是否可以从 Linq 调用方法?
- 我应该在同一个查询中执行所有操作吗?
这是我需要获取的信息的结构:
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;
S = someMethod(a.z).ToString()
行导致以下错误:
Unable to cast object of type 'System.Data.Linq.SqlClient.SqlColumn' to type 'System.Data.Linq.SqlClient.SqlMethodCall'.
最佳答案
您必须在 Linq-to-Objects
上下文中执行您的方法调用,因为在数据库端该方法调用没有意义 - 您可以使用 AsEnumerable()
- 基本上查询的其余部分将被评估为使用 Linq-to-Objects
的内存集合,您可以按预期使用方法调用:
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() })
关于c# - 在 Linq 查询中调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9970100/