C# 无法将Linq结果强制转换为现有预定义模型
我得到的错误如下C# 无法将Linq结果强制转换为现有预定义模型,c#,linq,C#,Linq,我得到的错误如下 //Able to get result from SP here List obj_Candidate_Geographies_List = DBEntity.sp_GetCandidateEntityDataByFunctionTypeIdAndLevel( Convert.ToInt32(FunctionTypeEnum.Geography), Convert.ToInt32(EntityLevel.Undefined)
//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; }
}
无法从“System.Collections.Generic.IEnumerable AnonymousType”转换为“System.Collections.Generic.IEnumerable WebUI.DataModels.CandidateGraphiesAttributes”
我的代码如下
//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结果强制转换为预定义数据模型列表。您应该创建
CanidateGeographicalCatributes
的实例,而不是让linq在此处为您创建匿名类型:
select new { CandidateId = c.CandidateId, FirstNam ...
改为:
select new CanidateGeographicAttributes() { CandidateId = c.CandidateId, FirstNam..
您需要创建新的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; }
}
IQueryable
obj\u候选对象\u地理位置\u属性\u列表=
(来自DBEntity.候选人中的c)
在c.CandidateId上的DBEntity.vw_Candidate_AttributesNew.ToList()中加入p
等于p.CandidateId
在c.CandidateId上的obj_候选人地理位置列表中加入q
等于q.CandidateId
选择新的候选地理信息属性
{
CandidateId=c.CandidateId,
FirstName=q.FirstName,
LastName=q.LastName,
GeographyId=q.GeographyId,
GeographyName=q.GeographyName,
SkillId=p.SkillId,
SkillName=p.SkillName,
CreatedOn=c.CreatedOn,
修饰词
}
).AsQueryable();
//预定义模型
公共类候选人地理奖
{
公共int候选ID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共int GeographyId{get;set;}
公共字符串GeographyName{get;set;}
公共int SkillId{get;set;}
公共字符串SkillName{get;set;}
public DateTime CreatedOn{get;set;}
公共日期时间修饰符{get;set;}
}
请花一些时间格式化此文件。我尝试过此操作,但出现编译错误“无法隐式转换类型”System.Collections.Generic.IEnumerablechange IQueryable为IEnumerable,或者您应该能够在linq语句末尾添加.AsQueryable(),以保持IQueryable