使用LINQ ToList的语法将GENERIC列表转换为其基本类型的列表 [英] Syntax using LINQ ToList to cast GENERIC list to list of its base type

查看:248
本文介绍了使用LINQ ToList的语法将GENERIC列表转换为其基本类型的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过很多关于ToList和泛型限制的stackoverflow Q& As,但是我还没有在最后的返回中找到解释Syntax Error的语法错误。为什么我必须显式选择 cast 元素(B)?

  public interface I1 
{
}
公共类C2:I1
{
public static列表与LT; I1> DowncastListA(列表< C2>列表)
{
//A:具体类有效。
返回列表== null? null:list.ToList< I1>();
}

public static List< I1> DowncastListB< T2>(列表< T2>列表)其中T2:I1
{
//B:WORKS,如果明确选择并投射每个元素。
返回列表== null? null:list.Select(a =>(I1)a).ToList();
}

public static List< I1> DowncastListC< T2>(List< T2> list)其中T2:I1
{
//C:语法错误:'List< T2>'不包含'ToList'的定义,最好的扩展方法过载ParallelEnumerable.ToList< I1>(ParallelQuery< I1>)'需要类型'ParallelQuery< I1>'
return list == null的接收器。 null:list.ToList< I1>();


code




一些相关的问题:

https://stackoverflow.com/a/1541173/199364 >
如何投射List< T> To List< ClassName>

解决方案

扩展方法 IEnumerable< T> .ToList< T>()不允许指定目标类型。 T 是源 IEnumerable 的类型(它可以从源集合中隐式知道)。



您可以使用它:

  public static List< I1> DowncastListC< T2>(List< T2> list)其中T2:I1 
{
return list == null? null:list.Cast< I1>()。ToList();
}

I。您首先投射每个元素(产生一个 IEnumerable< I1> ),然后根据该元素创建一个列表。

顺便说一句:你甚至可以使用这种扩展方法来简化它的使用:

  public static class扩展
{
public static List< I1> Downcast< T2>(此List< T2> list)其中T2:I1
{
return list == null? null:list.Cast< I1>()。ToList();
}
}


I've looked at lots of stackoverflow Q&As about ToList and generic constraints, but I haven't found one that explains the "Syntax Error" in the final return below. Why do I have to explicitly Select and cast the elements ("B")?

public interface I1
{
}
public class C2 : I1
{
    public static List<I1> DowncastListA( List<C2> list )
    {
        // "A": A concrete class works.
        return list == null ? null : list.ToList<I1>();
    }

    public static List<I1> DowncastListB<T2>( List<T2> list ) where T2 : I1
    {
        // "B": WORKS, if explicitly Select and Cast each element.
        return list == null ? null : list.Select( a => (I1)a ).ToList();
    }

    public static List<I1> DowncastListC<T2>( List<T2> list ) where T2 : I1
    {
        // "C": Syntax Error: 'List<T2>' does not contain a definition for 'ToList' and the best extension method overload 'ParallelEnumerable.ToList<I1>(ParallelQuery<I1>)' requires a receiver of type 'ParallelQuery<I1>'
        return list == null ? null : list.ToList<I1>();
    }
}


Some related Qs:
https://stackoverflow.com/a/1541173/199364
How to Cast List<T> To List<ClassName>

解决方案

The extension method IEnumerable<T>.ToList<T>() doesn't allow to specify a target type. T is the type of the source IEnumerable (which is implicitly known from the source collection).

Instead you can use this:

public static List<I1> DowncastListC<T2>( List<T2> list ) where T2 : I1
{
    return list == null ? null : list.Cast<I1>().ToList();
}

I.e. you first cast each element (resulting in an IEnumerable<I1>), then create a list from that.

BTW: you could even make that an extension method to simplify its usage:

public static class Extensions
{
    public static List<I1> Downcast<T2>(this List<T2> list) where T2 : I1
    {
        return list == null ? null : list.Cast<I1>().ToList();
    }
}

这篇关于使用LINQ ToList的语法将GENERIC列表转换为其基本类型的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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