我正试图将类型,但每次都会出现错误。
List<SubProduct> subProducts= Model.subproduct;
Model.subproduct
返回 IList<SubProduct>
。
我正试图将类型,但每次都会出现错误。
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>();
}
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
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. ...
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 ...
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 ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
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, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
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. ...