C# 将 IQueryable<> 类型对象转换为 List<T> 类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/755826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:57:51  来源:igfitidea点击:

Convert IQueryable<> type object to List<T> type?

c#listiqueryable

提问by Vikas

I have IQueryable<>object.

我有IQueryable<>对象。

I want to Convert it into List<>with selected columns like new { ID = s.ID, Name = s.Name }.

我想将它转换List<>为选定的列,如new { ID = s.ID, Name = s.Name }.

Edited

已编辑

Marc you are absolutely right!

马克你是完全正确的!

but I have only access to FindByAll()Method (because of my architecture).

但我只能访问FindByAll()Method (因为我的架构)。

And it gives me whole object in IQueryable<>

它给了我整个对象 IQueryable<>

And I have strict requirement( for creating json object for select tag) to have only list<>type with two fields.

而且我有严格的要求(为选择标签创建 json 对象)只能list<>输入两个字段。

采纳答案by Marc Gravell

Then just Select:

然后只是Select

var list = source.Select(s=>new { ID = s.ID, Name = s.Name }).ToList();

(edit) Actually - the names could be inferred in this case, so you could use:

(编辑)实际上 - 在这种情况下可以推断名称,因此您可以使用:

var list = source.Select(s=>new { s.ID, s.Name }).ToList();

which saves a few electrons...

这节省了一些电子......

回答by Brian Genisio

System.Linq has ToList() on IQueryable<> and IEnumerable<>. It will cause a full pass through the data to put it into a list, though. You loose your deferred invoke when you do this. Not a big deal if it is the consumer of the data.

System.Linq 在 IQueryable<> 和 IEnumerable<> 上有 ToList()。但是,它会导致完全通过数据以将其放入列表中。当你这样做时,你会失去你的延迟调用。如果它是数据的消费者,这没什么大不了的。

回答by Hannoun Yassir

Add the following:

添加以下内容:

using System.Linq

...and call ToList()on the IQueryable<>.

......并呼吁ToList()IQueryable<>

回答by Vedran

Here's a couple of extension methods I've jury-rigged together to convert IQueryables and IEnumerables from one type to another (i.e. DTO). It's mainly used to convert from a larger type (i.e. the type of the row in the database that has unneeded fields) to a smaller one.

这里有几个扩展方法,我已经组合在一起将 IQueryables 和 IEnumerables 从一种类型转换为另一种类型(即 DTO)。它主要用于将较大的类型(即数据库中具有不需要字段的行的类型)转换为较小的类型。

The positive sides of this approach are:

这种方法的积极方面是:

  • it requires almost no code to use - a simple call to .Transform<DtoType>() is all you need
  • it works just like .Select(s=>new{...}) i.e. when used with IQueryable it produces the optimal SQL code, excluding Type1 fields that DtoType doesn't have.
  • 它几乎不需要使用任何代码 - 只需简单调用 .Transform <DtoType>() 即可
  • 它的工作原理就像 .Select(s=>new{...}) 即当与 IQueryable 一起使用时,它会产生最佳的 SQL 代码,不包括 DtoType 没有的 Type1 字段。

LinqHelper.cs:

LinqHelper.cs:

public static IQueryable<TResult> Transform<TResult>(this IQueryable source)
{
    var resultType = typeof(TResult);
    var resultProperties = resultType.GetProperties().Where(p => p.CanWrite);

    ParameterExpression s = Expression.Parameter(source.ElementType, "s");

    var memberBindings =
        resultProperties.Select(p =>
            Expression.Bind(typeof(TResult).GetMember(p.Name)[0], Expression.Property(s, p.Name))).OfType<MemberBinding>();

    Expression memberInit = Expression.MemberInit(
        Expression.New(typeof(TResult)),
        memberBindings
        );

    var memberInitLambda = Expression.Lambda(memberInit, s);

    var typeArgs = new[]
        {
            source.ElementType, 
            memberInit.Type
        };

    var mc = Expression.Call(typeof(Queryable), "Select", typeArgs, source.Expression, memberInitLambda);

    var query = source.Provider.CreateQuery<TResult>(mc);

    return query;
}

public static IEnumerable<TResult> Transform<TResult>(this IEnumerable source)
{
    return source.AsQueryable().Transform<TResult>();
}

回答by mmmeff

The List class's constructor can convert an IQueryable for you:

List 类的构造函数可以为您转换 IQueryable:

public static List<TResult> ToList<TResult>(this IQueryable source)
{
    return new List<TResult>(source);
}

or you can just convert it without the extension method, of course:

或者你可以不使用扩展方法直接转换它,当然:

var list = new List<T>(queryable);