繁体   English   中英

无法投射 <TakeIterator> d__3a`1 [System.Object]键入System.Collections.Generic.IEnumerable

[英]Unable cast <TakeIterator>d__3a`1[System.Object] to type System.Collections.Generic.IEnumerable

我有一个将CSV文件读取到数据库的过程。 该csv超过600 Mb,因此我无法在内存中全部设置。 我使用通用模式来实现这一点,但在投射时遇到了问题:

在这里我阅读文件

using (var fs = File.OpenText(Path.Combine(FolderContainer, filename)))
{
    var csvConfiguration = new CsvConfiguration
    {
        HasHeaderRecord = true,
        Delimiter = ","
    };
    using (var csvReader = new CsvReader(fs, csvConfiguration))
    {
        csvReader.Configuration.RegisterClassMap(CsvMapping.CsvMapping.RetrieveMapType(type));
        var list = csvReader.GetRecords(type);
//            Console.WriteLine(list.First());
        dynamic repository = Activator.CreateInstance(typeof(Repository<>).MakeGenericType(type), UnitOfWork);
//            var activities = new Repository<Activity>(UnitOfWork);
        repository.InsertAllOnSubmit(list.Take(100));
        repository.SubmitChanges();
    }
}

我将Take(100)用于测试目的,将工作单位用于InMemory。

这是称为的存储库方法

public void InsertAllOnSubmit(IEnumerable<T> entities)
{
    _source.InsertAllOnSubmit(entities);
}

public void InsertAllOnSubmit(IEnumerable<object> entities)
{
//        foreach (var entity in entities)
//        {
//            InsertOnSubmit((T) entity);
//        }
    this.InsertAllOnSubmit((IEnumerable<T>)entities);
}

执行测试时,我有一个castException

System.InvalidCastException : Unable to cast object of type '<TakeIterator>d__3a`1[System.Object]' to type 'System.Collections.Generic.IEnumerable`1[KboOpenDataData.Activity]'.

我尝试在Take(100)之后添加AsEnumerable()或ToList(),如http://blog.codelab.co.nz/2009/12/22/unable-to-cast-object-of-类型问题/没有成功。 而且我不想使用ToList,因为由于大小原因我无法在内存中设置整个文件。

我评论了在存储库类中使用foreach的行,foreach可以运行,但是非常慢。

对成功施展有何建议?

尝试关注-

repository.InsertAllOnSubmit(list.Take(100).Cast<Activity>());

Cast扩展方法使您可以将可枚举类型从一种类型强制转换为另一种类型(当然,假定强制转换是有效的。这是您对显式强制转换所期望的)。

暂无
暂无

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

相关问题 无法将类型为“WhereListIterator`1[System.Object]”的对象转换为类型“System.Collections.Generic.IEnumerable`1[System.Int32]” 无法将类型为“ System.Collections.ArrayList”的对象转换为类型为“ System.Collections.Generic.IEnumerable” 无法将“System.Linq.OrderedEnumerable`2[***]”类型的对象转换为“System.Collections.Generic.IEnumerable`1[***]” 无法将类型为“ System.Data.SqlClient.SqlDataReader”的对象转换为类型为“ System.Collections.Generic.IEnumerable” 无法将类型为“ System.Data.Entity.Infrastructure.DbQuery`1 []”的对象转换为类型为“ System.Collections.Generic.IEnumerable”的对象 无法将类型为“ System.IO.FileSystemInfo []”的对象转换为类型为“ System.Collections.Generic.IEnumerable`1 [System.IO.DirectoryInfo] 合并两个EF查询,无法将类型为System.Data.Entity.Infrastructure.DbQuery的对象强制转换为System.Collections.Generic.IEnumerable 无法将类型为“ System.Data.Entity.DbSet” [City]的对象转换为类型为“ System.Collections.Generic.IEnumerable`1 [System.Web.Mvc.SelectListItem]”的对象 无法将类型为“System.Collections.Generic.List`1[System.Object]”的 object 转换为类型“System.Collections.Generic.List`1” 无法将 System.Collections.Generic.List[System.Object] 类型的对象转换为 System.Collections.Generic.IList[ShortCodeList]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM