试图转换的IList,以便列表中的AddRange [英] Trying to convert IList to List in order to AddRange
问题描述
我有一个的IList
。我尝试调用了ToList
然后的AddRange
。
不过,了ToList()
覆盖所有的结果。怎么会呢?
私人无效AddAliasesThatContainsCtid(字符串CTID,IList的< MamConfiguration_V1>结果)
{
...
的foreach(别名VAR化名)
{
VAR aliasId =@+别名;
。results.ToList()的AddRange(mMaMDBEntities.MamConfigurationToCTIDs_V1.Where(项目=> item.CTID == aliasId)
。选择(项目=> item.MamConfiguration_V1)
.ToList());
}
}
.ToList()
不转换的的IEnumerable< T>
到名单,其中,T>
,创建并返回充满枚举值一个新的列表
所以,你的 result.ToList()
将创建一个新的列表,并与一些数据填充它。但它不会改变由结果参数引用的对象的内容。
为了真正改变的结果的内容
参数,你必须使用它的。新增
方法或者如果你的设计允许它改变结果
到名单及类型LT; ..>
I have a IList
. I try to call ToList
and then AddRange
.
However, the ToList()
overwrite all the results. How come?
private void AddAliasesThatContainsCtid(string ctid, IList<MamConfiguration_V1> results)
{
...
foreach (var alias in aliases)
{
var aliasId = "@" + alias;
results.ToList().AddRange(mMaMDBEntities.MamConfigurationToCTIDs_V1.Where(item => item.CTID == aliasId)
.Select(item => item.MamConfiguration_V1)
.ToList());
}
}
.ToList()
does not convert an IEnumerable<T>
to a List<T>
, it creates and returns a new list filled with the values of the enumerable.
So your result.ToList()
will create a new list and fill it with some data. But it will not change the contents of the object referenced by the result parameter.
In order to actually change the contents of the result
parameter you have to use the .Add
method of it or if your design allows it change the type of result
to List<..>
.
这篇关于试图转换的IList,以便列表中的AddRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!