C# 将 IList 转换为列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2207341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:25:49  来源:igfitidea点击:

Cast IList to List

c#listcastingilist

提问by Pankaj

I am trying to cast IListtype to Listtype but I am getting error every time.

我正在尝试将IList类型转换为List类型,但每次都会出错。

List<SubProduct> subProducts= Model.subproduct;

Model.subproductreturns IList<SubProduct>.

Model.subproduct返回IList<SubProduct>

采纳答案by Pbirkoff

Try

尝试

List<SubProduct> subProducts = new List<SubProduct>(Model.subproduct);

or

或者

List<SubProduct> subProducts = Model.subproducts as List<SubProduct>;

回答by Webleeuw

List<SubProduct> subProducts= (List<SubProduct>)Model.subproduct;

The implicit conversion failes because List<>implements IList, not viceversa. So you can say IList<T> foo = new List<T>(), but not List<T> foo = (some IList-returning method or property).

隐式转换失败是因为List<>implements IList,反之亦然。所以你可以说IList<T> foo = new List<T>(),但不能List<T> foo = (some IList-returning method or property)

回答by Mark Seemann

How about this:

这个怎么样:

List<SubProduct> subProducts = Model.subproduct.ToList();

回答by bluish

In my case I had to do this, because none of the suggested solutions were available:

就我而言,我必须这样做,因为没有任何建议的解决方案可用:

List<SubProduct> subProducts = Model.subproduct.Cast<SubProduct>().ToList();

回答by Hudhaifa Yoosuf

List<ProjectResources> list = new List<ProjectResources>();        
IList<ProjectResources> obj = `Your Data Will Be Here`;
list = obj.ToList<ProjectResources>();

This Would Convert IList Object to List Object.

这会将 IList 对象转换为 List 对象。

回答by Ghebrehiywet

This is the best option to cast/convert list of generic object to list of string.

这是将通用对象列表转换/转换为字符串列表的最佳选择。

object valueList;
List<string> list = ((IList)valueList).Cast<object>().Select(o => o.ToString()).ToList();

回答by user11331434

The other answers all recommend to use AddRange with an IList.

其他答案都建议将 AddRange 与 IList 一起使用。

A more elegant solution that avoids the casting is to implement an extension to IList to do the job.

避免强制转换的更优雅的解决方案是实现对 IList 的扩展来完成这项工作。

In VB.NET:

在 VB.NET 中:

<Extension()>
Public Sub AddRange(Of T)(ByRef Exttype As IList(Of T), ElementsToAdd As IEnumerable(Of T))
   For Each ele In ElementsToAdd
      Exttype.Add(ele)
   Next
End Sub

And in C#:

在 C# 中:

public void AddRange<T>(this ref IList<T> Exttype, IEnumerable<T> ElementsToAdd)
{
    foreach (var ele in ElementsToAdd)
    {
        Exttype.Add(ele);
    }
}

回答by JasonLandbridge

If you have an IList containing interfaces, you can cast it like this:

如果你有一个包含接口的 IList,你可以像这样转换它:

List to IList

列表到 IList

List<Foo> Foos = new List<Foo>(); 
IList<IFoo> IFoos = Foos.ToList<IFoo>();

IList to List

IList到列表

IList<IFoo> IFoos = new List<IFoo>();
List<Foo> Foos = new List<Foo>(IFoos.Select(x => (Foo)x));

This assumes Foohas IFoointerfaced.

这假设Foo已经IFoo接口了。

回答by Golden Lion

    public async Task<List<TimeAndAttendanceShift>> FindEntitiesByExpression(Expression<Func<TimeAndAttendanceShift, bool>> predicate)
    {
        IList<TimeAndAttendanceShift> result = await _dbContext.Set<TimeAndAttendanceShift>().Where(predicate).ToListAsync<TimeAndAttendanceShift>();

        return result.ToList<TimeAndAttendanceShift>();
    }