.Net 4.0 无法将类型 'System.Collections.Generic.List`1[ClassName]' 的对象转换为类型 'ClassName'
.Net 4.0 Unable to cast object of type 'System.Collections.Generic.List`1[ClassName]' to type 'ClassName'
public class SkillView {
public string Name { get; set; }
public IList<SkillValueView> Selected { get; set; }}
public class SkillValueView {
public string APlan { get; set; }
public DateTime? DueDate { get; set; }}
private IList<SkillValueView> MapToSkillValueList(IGrouping<string, GetSession_Result> query)
{
var actionItem = query.FirstOrDefault();
List<SkillValueView> LSVV = new List<SkillValueView>();
SkillValueView SVV = new SkillValueView();
for (int i = 0; i < actionItem.NoOf; i++)
{
SVV.APlan = actionItem.ExAPlan;
SVV.DueDate = actionItem.ExDueDate;
LSVV.Add(new SkillValueView() { APlan = SVV.APlan, DueDate = SVV.DueDate });
}
}
return LSVV;
}
private SkillView MapToSkill(IGrouping<int, GetSession_Result> query){
var skill = query.FirstOrDefault();
return new SkillView()
{
Name = skill.SkillName,
Selected = query.GroupBy(s => s.ExAPlan).Select(MapToSkillValueList).ToList()
};
}
我无法映射 >Selected = query.GroupBy(s => s.ExAPlan).Select(MapToSkillValueList).ToList () 并出现错误 无法将类型 'System.Collections.Generic.List`1[IsaiX.Coach.Models.SkillValueView]' 的对象转换为类型 'IsaiX.Coach.Models.SkillValueView'。
在这种情况下,您应该使用 SelectMany
而不是 Select
。
query.GroupBy(s => s.ExAPlan).SelectMany(MapToSkillValueList).ToList().
Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence
public class SkillView {
public string Name { get; set; }
public IList<SkillValueView> Selected { get; set; }}
public class SkillValueView {
public string APlan { get; set; }
public DateTime? DueDate { get; set; }}
private IList<SkillValueView> MapToSkillValueList(IGrouping<string, GetSession_Result> query)
{
var actionItem = query.FirstOrDefault();
List<SkillValueView> LSVV = new List<SkillValueView>();
SkillValueView SVV = new SkillValueView();
for (int i = 0; i < actionItem.NoOf; i++)
{
SVV.APlan = actionItem.ExAPlan;
SVV.DueDate = actionItem.ExDueDate;
LSVV.Add(new SkillValueView() { APlan = SVV.APlan, DueDate = SVV.DueDate });
}
}
return LSVV;
}
private SkillView MapToSkill(IGrouping<int, GetSession_Result> query){
var skill = query.FirstOrDefault();
return new SkillView()
{
Name = skill.SkillName,
Selected = query.GroupBy(s => s.ExAPlan).Select(MapToSkillValueList).ToList()
};
}
我无法映射 >Selected = query.GroupBy(s => s.ExAPlan).Select(MapToSkillValueList).ToList () 并出现错误 无法将类型 'System.Collections.Generic.List`1[IsaiX.Coach.Models.SkillValueView]' 的对象转换为类型 'IsaiX.Coach.Models.SkillValueView'。
在这种情况下,您应该使用 SelectMany
而不是 Select
。
query.GroupBy(s => s.ExAPlan).SelectMany(MapToSkillValueList).ToList().
Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence