【问题标题】:Converting List<BaseType> to List<DerivedType> with performance in mind考虑到性能,将 List<BaseType> 转换为 List<DerivedType>
【发布时间】:2017-05-24 09:02:13
【问题描述】:

我有一个非常密集的例程,我是它的客户,它返回一个 List&lt;BaseType&gt; 给我。除其他参数外,该例程具有DateTime,但返回的List&lt;BaseType&gt; 不包含此信息:BaseType 没有可以跟踪此信息的DateTime 成员。 (这是因为该例程被设计为仅在一个日期调用,在 VBA 中执行的多个日期循环。)

现在我需要这个额外的日期信息,因为我想在一个List&lt;BaseType&gt; 中连接许多不同的List&lt;BaseType&gt;(以不同的日期生成),我想对其应用一些需要知道日期的处理。

所以我实现了一个简单的类DerivedType,它派生自BaseType,并结合了这个日期时间的附加信息。

我想知道将List&lt;BaseType&gt;“转换”为List&lt;DerivedType&gt; 的最佳方式(就性能而言,最好没有副本)是什么,其中所有DerivedTypeDateTime 成员具有相同的值.

备注。 我想同时将所有生成的List&lt;DerivedType&gt; 放在一个List&lt;DerivedType&gt; 中,我将不得不使用List 的操作对其进行修剪。这就是我为什么要坚持List的原因。

澄清编辑

上下文:在 VBA 中,我循环了许多日期,由于c# dll,我为每个日期生成了一个数组,最后,我在某处打印(在 VBA 中)所有这些数组。在 c# 中生成一个数组使用上述例程(我无法更改,因为它依赖于旧版 dll):List&lt;BaseType&gt; 是在日期前生成的,并以与日期相关的方式处理以生成数组。我想要做的是从 VBA 中删除日期循环并在 c# 中执行此操作。由于“处理”实际上取决于List&lt;BaseType&gt; 的列表性质和日期,BaseType 是否包含日期信息,我可以将所有List&lt;BaseType&gt; 连接到一个唯一的List&lt;BaseType&gt;,我将应用处理,而不是逐个列表应用它。但是BaseType 没有包含日期​​信息,所以我被卡住了......这导致了这个问题。

【问题讨论】:

  • 很遗憾,您不能这样做,因为List&lt;T&gt; 允许代码将项目添加到列表以及从列表中提取项目。如果您假设列表仅包含派生类型的对象,则提取项目的代码可能会获取基本类型或从该基本类型下降的任何其他类型,这将不会 i> 成为派生类型。你别无选择,只能进行转换,得到一个新的集合对象。
  • Damien 是对的,它非常不清楚你想要什么。要将 List 的元素转换为另一种类型,只需使用 List.Cast&lt;T&gt;()
  • 要获得一个只包含DerivedType 类型的对象的新列表,请使用list.OfType&lt;DerivedType&gt;().ToList()
  • 如果所有对象都具有相同的DateTime 信息,请使用DateTime time = baseTypeList.OfType&lt;DerivedType&gt;.First().DateTimeProperty;。现在您不需要转换其他对象,因为您不需要了解更多信息。
  • @TimSchmelter 这是我曾经以最佳方式生成baseTypeList

标签: c# inheritance generic-list


【解决方案1】:

这是一种不进行强制转换的替代方法。您仍然需要处理列表列表的列表...

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{

    public class BaseClass
    {
    }

    public class BaseClassList : List<BaseClass>
    {
        public DateTime CallDate { get; set; }

        public BaseClassList(IEnumerable<BaseClass> elements, DateTime CallDate)
            : base(elements)
        {
            this.CallDate = CallDate;
        }
    }

    public static class BaseClassExtensions
    {
        public static DateTime Date(this BaseClass ext, BaseClassList bcl)
        {
            return bcl.CallDate;
        }
    }

    class Program
    {
        public static List<BaseClass> Func(DateTime d)
        {
            BaseClass A = new BaseClass();
            BaseClass B = new BaseClass();
            return new List<BaseClass>() { A, B };
        }

        static void Main(string[] args)
        {
            DateTime d1 = new DateTime(2017, 5, 1);
            DateTime d2 = new DateTime(2018, 5, 1);
            List<DateTime> dates = new List<DateTime>() { d1, d2 };


            List<BaseClassList> myList = new List<BaseClassList>();

            foreach (DateTime d in dates)
                myList.Add(new BaseClassList(Func(d), d));

            foreach(var el in myList)
                foreach (BaseClass bc in el)
                    Console.WriteLine(bc.Date(el));
        }
    }
}

【讨论】:

  • 我真的需要解释的列表
猜你喜欢
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 2020-11-27
  • 2019-05-20
相关资源
最近更新 更多