ElementAt() 在 Linq to SubSonic 中不起作用
我有这个查询:
var iterator = criteria.binaryAssetBranchNodeIds.GetEnumerator();
iterator.MoveNext();
var binaryAssetStructures = from bas in db.BinaryAssetStructures
where bas.BinaryAssetStructureId == iterator.Current
select bas;
当我使用 foreach 循环迭代 binaryAssetStructureIds 时,没有出现问题。当我尝试此操作时,
var binaryAssetStructure = binaryAssetStructures.ElementAt(0);
出现以下错误:
Unable to cast object of type 'System.Linq.Expressions.MethodCallExpression' to type 'SubSonic.Linq.Structure.ProjectionExpression'
First() for example does work...我在这里缺少什么...
I have this query:
var iterator = criteria.binaryAssetBranchNodeIds.GetEnumerator();
iterator.MoveNext();
var binaryAssetStructures = from bas in db.BinaryAssetStructures
where bas.BinaryAssetStructureId == iterator.Current
select bas;
When I iterate over the binaryAssetStructureIds with a foreach loop no problems occur. When I try this
var binaryAssetStructure = binaryAssetStructures.ElementAt(0);
I get following error:
Unable to cast object of type 'System.Linq.Expressions.MethodCallExpression' to type 'SubSonic.Linq.Structure.ProjectionExpression'
First() for example does work... What am I missing here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我根本不知道 SubSonic,但 FWIW 实体框架也存在类似的问题。在这种情况下,归根结底是因为 ElementAt 没有直接转换为 SQL。
First()
可以轻松翻译为SELECT TOP 1 FROM ... ORDER BY ...
,但对于ElementAt
则不容易表达相同的内容>。您可能会争辩说,例如
ElementAt(5)
应该翻译为SELECT TOP 5 FROM ... ORDER BY ...
,然后简单地丢弃前四个元素,但那如果您要求ElementAt(100000)
,则效果不太好。在 EF 中,您可以部分克服此问题,强制首先计算表达式,这可以通过调用
AsEnumerable
、ToList
或ToArray
来完成。例如,
我希望这会有所帮助,尽管没有明确针对 SubSonic。
I don't know SubSonic at all, but FWIW a similar issue exists with the Entity Framework. In that case it boils down to the fact that there's no direct translation of ElementAt to SQL.
First()
can be easily translated toSELECT TOP 1 FROM ... ORDER BY ...
, but the same is not easily expressed forElementAt
.You could argue that e.g.
ElementAt(5)
should be translated toSELECT TOP 5 FROM ... ORDER BY ...
and then the first four elements simply discarded, but that doesn't work very well if you ask forElementAt(100000)
.In EF, you can partialle overcome this issue forcing the expression to be evaluated first, which can be done with calls to
AsEnumerable
,ToList
orToArray
.For example
I hope this helps although not explicitly directed at SubSonic.