本文介绍了添加列表&LT; T&GT;。新增()另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个的IEnumerable< TravelDetails> ,我尝试添加瓦莱斯在为 -loop到一个列表与LT; TravelDetails> 。我不断收到错误。I have an IEnumerable<TravelDetails> and I am trying to add the vales in the for-loop to a List<TravelDetails>. I keep getting the errors.错误15参数1:无法从System.Collections.Generic.List'到'TrafficCore.DataObjects.TripDetails'C转换:\\ \\TrafficNew\TI 511-Web\Traffic 2.0\511Traffic\511Traffic\Models\DrivingTime.cs我的代码是List<TripDetails> tripDetailsCollection = new List<TripDetails>();foreach (DrivingTimeRoute dtr in dtRoutes){ foreach (Trip trip in dtr.Trips) { foreach (TripPathLink tpl in trip.TripPathLinks) { tplCollection.Add(tpl); } IEnumerable<TripDetails> tripDetails = //long Linq-to-Sql here List<TripDetails> td = tripDetails.ToList(); tripDetailsCollection.Add(td); // <<< Error here }}有一个人帮助我。 谢谢,爬完Can some one help me with this.Thanks,Pawan推荐答案的 列表< T>。新增 增加一个元素。相反,使用 列表< T> .AddRange 添加多个值List<T>.Add adds a single element. Instead, use List<T>.AddRange to add multiple values.此外,列表< T> .AddRange 需要一个的IEnumerable< T> ,所以你不需要 tripDetails 转换成列表< TripDetails> ,你可以直接通过它,例如:Additionally, List<T>.AddRange takes an IEnumerable<T>, so you don't need to convert tripDetails into a List<TripDetails>, you can pass it directly, e.g.:tripDetailsCollection.AddRange(tripDetails); 这篇关于添加列表&LT; T&GT;。新增()另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-15 15:35