我收到如下错误
cannot convert from 'System.Collections.Generic.IEnumerable AnonymousType#1' to 'System.Collections.Generic.IEnumerable WebUI.DataModels.CandidateGeographiesAttributes'
我的代码如下:
//Able to get result from SP here
List obj_Candidate_Geographies_List =
DBEntity.sp_GetCandidateEntityDataByFunctionTypeIdAndLevel(
Convert.ToInt32(FunctionTypeEnum.Geography),
Convert.ToInt32(EntityLevel.Undefined)
).ToList();
//Not able to convert to predefined model here
IQueryable obj_Candidate_Geographies_Attributes_List =
(from c in DBEntity.Candidates.ToList()
join p in DBEntity.vw_Candidate_AttributesNew.ToList()
on c.CandidateId equals p.CandidateId
join q in obj_Candidate_Geographies_List
on c.CandidateId equals q.CandidateId
select new
{
CandidateId = c.CandidateId,
FirstName = q.FirstName,
LastName = q.LastName,
GeographyId = q.GeographyId,
GeographyName = q.GeographyName,
SkillId = p.SkillId,
SkillName = p.SkillName,
CreatedOn = c.CreatedOn,
ModifiedOn = c.ModifiedOn
}
);
//Predefined Model
public class CandidateGeographiesAttributes
{
public int CandidateId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int GeographyId { get; set; }
public string GeographyName { get; set; }
public int SkillId { get; set; }
public string SkillName { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime ModifiedOn { get; set; }
}
有没有人可以帮我解决如何将linq结果转换为预定义数据模型列表。
答案 0 :(得分:1)
您应该创建CanidateGeographicAttributes
的实例,而不是让linq为您创建匿名类型:
select new { CandidateId = c.CandidateId, FirstNam ...
更改为:
select new CanidateGeographicAttributes() { CandidateId = c.CandidateId, FirstNam..
答案 1 :(得分:1)
您需要创建新的CandidateGeographiesAttributes而不是匿名类型。就这样:
IQueryable<CandidateGeographiesAttributes>
obj_Candidate_Geographies_Attributes_List =
(from c in DBEntity.Candidates
join p in DBEntity.vw_Candidate_AttributesNew.ToList() on c.CandidateId
equals p.CandidateId
join q in obj_Candidate_Geographies_List on c.CandidateId
equals q.CandidateId
select new CandidateGeographiesAttributes
{
CandidateId = c.CandidateId,
FirstName = q.FirstName,
LastName = q.LastName,
GeographyId = q.GeographyId,
GeographyName = q.GeographyName,
SkillId = p.SkillId,
SkillName = p.SkillName,
CreatedOn = c.CreatedOn,
ModifiedOn = c.ModifiedOn
}
).AsQueryable();
//Predefined Model
public class CandidateGeographiesAttributes
{
public int CandidateId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int GeographyId { get; set; }
public string GeographyName { get; set; }
public int SkillId { get; set; }
public string SkillName { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime ModifiedOn { get; set; }
}