English 中文(简体)
Cast IList to List
原标题:Cast IList to List

我正试图将类型转换为类型,但每次都会出现错误。

List<SubProduct> subProducts= Model.subproduct;

Model.subproduct 返回 IList<SubProduct>

最佳回答

尝试 (chángshì)

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

或者

List<SubProduct> subProducts = Model.subproducts as List<SubProduct>;
问题回答

这怎么样?

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

就我而言,我不得不这样做,因为没有建议的解决办法:

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

隐式转换失败,因为List<>实现了IList,而不是反过来。因此,您可以说IList<T> foo = new List<T>(),但不能说List<T> foo =(一些返回IList的方法或属性)

如果您有一个包含接口的IList,可以这样进行转换:

列表到I列表

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

IList 转为 List

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

假定Foo已实现了IFoo接口。

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

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

其他答复都建议使用AshRange和IList。

一种更优雅的解决方案是实现一个扩展方法来完成任务,避免了强制转换。

在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

在C#中:

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签