0

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>();
    }
4

2 回答 2

0

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>();
}
于 2013-10-13T12:15:13.660 回答
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();
于 2013-10-13T12:16:47.753 回答