在LinQ C#中修复'Take'扩展方法

时间:2012-03-29 21:02:40

标签: c#-4.0

下面是我的代码片段,我想从表格中只显示10条记录。

AdventureWorksDataContext adc = new AdventureWorksDataContext();    
var query1 = from customers in adc.Customers
                             orderby (customers.FirstName).Take(10)
                             select new
                             {
                                 customers.FirstName,
                                 customers.LastName,
                                 customers.CustomerAddresses
                             };
 foreach (var v in query1)   -> Line 23
            {
                Console.Write(v.FirstName);
                Console.Write("  ");
                Console.Write(v.LastName);
                Console.Write("       ");
                Console.Write(v.CustomerAddresses);
            }

我收到以下运行时错误:

Unhandled Exception: System.InvalidOperationException: Cannot order by type 'System.Collections.Generic.IEnumerable`1[System.Char]'.
   at System.Data.Linq.SqlClient.QueryConverter.VisitOrderBy(Expression sequence, LambdaExpression expression, SqlOrderType orderType)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(Method CallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpres
sion mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence,
 LambdaExpression selector)
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(Method
CallExpression mc)
   at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpres
sion 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, SqlNod
eAnnotations annotations)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider
.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.Get
Enumerator()
   at LabAssignment4Part2.AdventureWorks.Main(String[] args) in E:\AdventureWorks.cs:line 23

我想我没有以正确的方式实现Take方法。

请帮助我。

1 个答案:

答案 0 :(得分:1)

AdventureWorksDataContext adc = new AdventureWorksDataContext();    
var query1 = (from customers in adc.Customers
                         orderby (customers.FirstName)
                         select new
                         {
                             customers.FirstName,
                             customers.LastName,
                             customers.CustomerAddresses
                         }).Take(10);
foreach (var v in query1)
        {
            Console.Write(v.FirstName);
            Console.Write("  ");
            Console.Write(v.LastName);
            Console.Write("       ");
            Console.Write(v.CustomerAddresses);
        }

编辑: 应该在查询对象上作为一个整体而不是在orderby子句上进行。你也可以说foreach (var v in query1.Take(10))并且它是有效的。