为什么查询运算符“的ElementAt”LINQ中不支持到SQL? [英] Why is the query operator 'ElementAt' is not supported in LINQ to SQL?

查看:528
本文介绍了为什么查询运算符“的ElementAt”LINQ中不支持到SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在LINQ到SQL,我得到的异常的查询经营者的ElementAt的不支持。的当尝试使用上一个IQueryable的ElementAt的扩展方法从LINQ返回SQL查询。



下面是堆栈跟踪:

 在System.Data这。 Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression MC)
在System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression MC)
在System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression节点)
在System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression节点)
在System.Data.Linq.SqlClient.SqlProvider.BuildQuery(表达式查询,SqlNodeAnnotations注释)
。在系统.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式查询)
在System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute [TResult](表达式表达式)
在System.Linq.Queryable.ElementAt [TSource](IQueryable`1源的Int32指数)

现在我意识到摆脱这种例外和使用的ElementAt我可以使用扩展方法之前称之为.ToList()',它会工作。这是好的,但我还是不喜欢的事实,这是一个运行时异常(什么好像LSP违规)。



时有一个原因这些方法能不支持?难道仅仅是因为他们不能很容易地转换成SQL?还有什么其他的IQueryable / IEnumerable的扩展方法不被支持,有一个列表的地方?



这将是很好,以避免运行时异常。


< DIV CLASS =h2_lin>解决方案

从MSDN,标准查询运算符转换(的LINQ to SQL) - 本文包含尚未翻译操作的完整列表:




  • TakeWhile,SkipWhile

  • 反向

  • 最后,LastOrDefault

  • 的ElementAt,ElementAtOrDefault

  • DefaultIfEmpty




运营商没有翻译



以下方法不是
。通过LINQ转换为SQL。最
常见的原因是无序的多集和
序列之间的差异



运营商



依据



...



的ElementAt
ElementAtOrDefault



SQL查询在多重集操作,对可转位序列不是



In LINQ to SQL, I get the exception "The query operator 'ElementAt' is not supported." When trying to use the ElementAt extension method on an IQueryable returned from a LINQ to SQL query.

Here is the stack trace:

at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
   at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.ElementAt[TSource](IQueryable`1 source, Int32 index)

Now I realize to get rid of this exception and to use ElementAt I could call '.ToList()' before using the extension method and it will work. That is fine, but I still don't like the fact that this is a runtime exception (and what seems like LSP violation).

Is there a reason why these methods cannot be supported? Is it just because they cannot be translated easily into SQL? What other IQueryable/IEnumerable extension methods are not supported, is there a list somewhere?

It would be nice to avoid runtime exceptions.

解决方案

From MSDN, Standard Query Operator Translation (LINQ to SQL) - this article contains the full list of operators that haven't been translated:

  • TakeWhile , SkipWhile
  • Reverse
  • Last , LastOrDefault
  • ElementAt , ElementAtOrDefault
  • DefaultIfEmpty

Operators with No Translation

The following methods are not translated by LINQ to SQL. The most common reason is the difference between unordered multisets and sequences.

Operators

Rationale

...

ElementAt , ElementAtOrDefault

SQL queries operate on multisets, not on indexable sequences.

这篇关于为什么查询运算符“的ElementAt”LINQ中不支持到SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆