net我正在.net中开发一个应用程序。
我收到以下错误,我不知道我的代码有什么问题
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
这是我的代码,异常即将来临。
public List<ToureDeieselBean> loadDeiselListOnTourUpDownId(int tourUpDownId)
{
return (from p in context.tb_tourDiesel
where p.deleted == 0
where p.tourUpDownId == tourUpDownId
select new ToureDeieselBean
{
id = p.id,
qty =(float)p.qty,
ratePerlt = (float)p.ratePerlt,
createdBy = p.createdBy,
createdOn = p.createdOn==null?"":(p.createdOn).ToString(),
updatedBy = p.updatedBy,
updatedOn = p.updatedOn == null ? "" :(p.updatedOn).ToString(),
descrption=p.descrption,
deleted=Convert.ToBoolean(p.deleted)
}).ToList<ToureDeieselBean>();
}
答案 0 :(得分:0)
您可以使用null coalescing operator删除ToString
及更长的三元运算符:
public List<ToureDeieselBean> loadDeiselListOnTourUpDownId(int tourUpDownId)
{
return (from p in context.tb_tourDiesel
where p.deleted == 0
where p.tourUpDownId == tourUpDownId
select new ToureDeieselBean
{
id = p.id,
qty =(float)p.qty,
ratePerlt = (float)p.ratePerlt,
createdBy = p.createdBy,
createdOn = p.createdOn ?? "",
updatedBy = p.updatedBy,
updatedOn = p.updatedOn ?? "",
descrption=p.descrption,
deleted=Convert.ToBoolean(p.deleted)
}).ToList<ToureDeieselBean>();
}
答案 1 :(得分:0)
当您使用EF LINQ时,查询被转换为SQL,当您在LINQ中使用SQL中不存在的此类方法时,您会遇到此异常。
这将有效:
var temp=(from p in context.tb_tourDiesel
where p.deleted == 0
where p.tourUpDownId == tourUpDownId).ToList<ToureDeieselBean>();
return (from p in temp
select new ToureDeieselBean
{
id = p.id,
qty = (float)p.qty,
ratePerlt = (float)p.ratePerlt,
createdBy = p.createdBy,
createdOn = p.createdOn == null ? "" : (p.createdOn).ToString(),
updatedBy = p.updatedBy,
updatedOn = p.updatedOn == null ? "" : (p.updatedOn).ToString(),
descrption = p.descrption,
deleted = Convert.ToBoolean(p.deleted)
}).ToList();