0

net and i am developing an application in .net.

I am getting the following error and i don't know what is wrong with my code

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

This is my code where exception is coming.

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>();
    }
ap.singh
  • 1,150
  • 4
  • 15
  • 35
  • 1
    Change `createdOn` and `updatedOn` to DateTime types and don't use `ToString()` (which does not work in all Linq providers) – Joachim Isaksson Oct 13 '13 at 12:14
  • You can use method syntax without any problem. IMHO LINQ does not recognize ToString() in query sntax. – Farhad Jabiyev Oct 13 '13 at 12:16
  • @ Joachim Isaksson if i change createdOn and updatedOn to DateTime then it not accepts the null value – ap.singh Oct 13 '13 at 12:23
  • Then just use `DateTime?`. BTW: You will probably also have to change the type of `deleted` because most likely `Convert.ToBoolean` will throw the next exception. – Slauma Oct 13 '13 at 12:26
  • @ Slauma yes deleted was also throwing exception before but how to fix that. – ap.singh Oct 13 '13 at 12:27
  • Make `ToureDeieselBean.deleted` the same type as `p.deleted`. – Slauma Oct 13 '13 at 12:31
  • I have tried that also But that is also throwing an exception "Can not implicitly convert type 'byte?' to 'byte'. An explicit conversion exists (are you missing a cast?) " – ap.singh Oct 13 '13 at 12:36
  • Well, it looks that `ToureDeieselBean.deleted` should be of type `byte?` then, not just `byte`. – Slauma Oct 13 '13 at 12:38
  • no its public byte deleted { get; set; } – ap.singh Oct 13 '13 at 12:40

2 Answers2

0

You can remove the ToString and longer ternary operator by using the null coalescing operator:

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>();
}
slugster
  • 49,403
  • 14
  • 95
  • 145
0

When you work with EF LINQ query is translated into SQL, and when you use such methods in LINQ which doesn't exist in SQL you get this exception.

This will work:

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();
Lev
  • 3,719
  • 6
  • 41
  • 56