IQueryable的列出 [英] IQueryable to List

查看:274
本文介绍了IQueryable的列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 System.Linq.Dynamic 来让我动态地选择名单从这样的查询字段:

  finalQuery = query.Select(的String.Format(新的({0}),的string.join(,,选择器))); 



其中,选择只是一个列表<串GT; 所有我想要的字段。这个伟大的工程,但这个版本的扩展方法选择的返回的IQueryable 。这是不是的不可以 的IQueryable< T> 。如果我有一个的IQueryable< T> 我可以简单地做一个 .ToList()将其转换成一个列表,并强制查询实际上在数据库上运行,但与非通用 IQueryable的,该方法不存在。



这是因为了ToList 继承了IEnumerable< T> 其中的IQueryable< T> 继承和的IQueryable ,很明显,没有



那么,什么是最有效的方式得到一个的IQueryable 来执行查询并给我回一个列表?我可以这样做:

 列表<对象> RTN =新的List<对象>(); 
的foreach(在finalQuery变种O)
{
rtn.Add(O);
}



但似乎他们应该有一种更简单的方法。



编辑:为了应对建议,我都试过:

  finalQuery.Cast<对象>()了ToList(); 

  finalQuery.Cast<动态方式>()了ToList(); 



这既给 NotSupportedExceptions 的消息:

 无法转换类型'DynamicClass1为键入'System.Object的。 LINQ到实体
只支持铸造EDM原始或枚举类型。


解决方案

这似乎是在路上LINQ一个限制实体转化 IQueryable.Cast 匿名类型。您可以使用它作为一个的IEnumerable (你工作的例子做到这一点)解决此问题。这将导致代码做在.NET运行时中投的它是从数据库中检索后,而不是试图处理它在数据库引擎。例如:

 的IEnumerable finalQuery = query.Select(的String.Format(新的({0}),
串。加入(,,选择器)));
VAR的结果= finalQuery.Cast<动态>()了ToList()。

 公共静态的IList< T> CastToList< T>(这个IEnumerable的源)
{
返回新的List< T>(source.Cast< T>());
}

变种finalQuery = query.Select(的String.Format(新({0}),
的string.join(,,选择器)));
VAR的结果= finalQuery.CastToList<动态>();


I'm using System.Linq.Dynamic to allow me to dynamically select a list of fields from a query like this:

finalQuery = query.Select(string.Format("new({0})", string.Join(",", selectors)));

Where selectors is just a List<string> with all the fields I want. This works great, but this version of the extension method Select returns an IQueryable. Not this is not IQueryable<T>. If I have an IQueryable<T> I can simply do a .ToList() to convert it into a list and force the query to actually run on the database, but with the non-generic IQueryable, that method doesn't exists.

This is because ToList is inherited from IEnumerable<T> which IQueryable<T> inherits and IQueryable, obviously, doesn't.

So what's the most efficient way to get an IQueryable to execute the query and give me back a list? I can do this:

List<object> rtn = new List<object>();
foreach (var o in finalQuery)
{
    rtn.Add(o);
}

But it seems like their ought to be an easier way.

Edit: In response to suggestions, I tried both:

finalQuery.Cast<object>().ToList();

and:

finalQuery.Cast<dynamic>().ToList();

Which both give NotSupportedExceptions with the message:

Unable to cast the type 'DynamicClass1' to type 'System.Object'. LINQ to Entities 
only supports casting EDM primitive or enumeration types.

解决方案

This appears to be a limitation in the way LINQ to Entities translates IQueryable.Cast with anonymous types. You can work around this by using it as an IEnumerable (your working example does this). This causes the code to do the cast in the .NET runtime after it's retrieved from the DB, instead of trying to handle it in the DB engine. E.g.

IEnumerable finalQuery = query.Select(string.Format("new({0})",
                                                   string.Join(",", selectors)));
var result = finalQuery.Cast<dynamic>().ToList();

Or

public static IList<T> CastToList<T>(this IEnumerable source)
{
    return new List<T>(source.Cast<T>());
}

var finalQuery = query.Select(string.Format("new({0})",
                                            string.Join(",", selectors)));
var result = finalQuery.CastToList<dynamic>();

这篇关于IQueryable的列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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