简体   繁体   English

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

[英]Convert IQueryable<> type object to List<T> type?

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()方法(因为我的架构)。

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. 我有严格的要求(为select标签创建json对象)只有list<> type有两个字段。

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... 这节省了一些电子......

Add the following: 添加以下内容:

using System.Linq

...and call ToList() on the IQueryable<> . ...并在IQueryable<>上调用ToList()

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);

System.Linq has ToList() on IQueryable<> and IEnumerable<>. System.Linq在IQueryable <>和IEnumerable <>上有ToList()。 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. 如果它是数据的消费者,那没什么大不了的。

Here's a couple of extension methods I've jury-rigged together to convert IQueryables and IEnumerables from one type to another (ie DTO). 这里有几种扩展方法,我已经陪审团将IQueryables和IEnumerables从一种类型转换为另一种类型(即DTO)。 It's mainly used to convert from a larger type (ie the type of the row in the database that has unneeded fields) to a smaller one. 它主要用于从较大的类型(即数据库中具有不需要的字段的行的类型)转换为较小的类型。

The positive sides of this approach are: 这种方法的积极方面是:

  • it requires almost no code to use - a simple call to .Transform <DtoType> () is all you need 它几乎不需要使用任何代码 - 只需简单调用<DtoType> ()就可以了
  • it works just like .Select(s=>new{...}) ie when used with IQueryable it produces the optimal SQL code, excluding Type1 fields that DtoType doesn't have. 它就像.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>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM