试图转换的IList,以便列表中的AddRange [英] Trying to convert IList to List in order to AddRange

查看:169
本文介绍了试图转换的IList,以便列表中的AddRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的IList 。我尝试调用了ToList 然后的AddRange

不过,了ToList()覆盖所有的结果。怎么会呢?

 私人无效AddAlias​​esThatContainsCtid(字符串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屋!

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