none
Method 'Int32 Parse(System.String)' has no supported translation to SQL. RRS feed

  • Question

  • Hi,

    My code is shown here

    private static LWorkDataContext lwdc1
          = new LWorkDataContext();
    
    var retiredQuery =
                from rq in lwdc1.Employees
                let workYear = DateTime.Today.Year - Int32.Parse(rq.BORNDATE.Substring(0, 4))
                let retireAge = rq.SEX == "M" ? 60 : 55
                where retireAge - workYear < 0
                select rq;

    I try to display results into DataGridView, but when I run the query , an exception is thrown:

    Method 'Int32 Parse(System.String)' has no supported translation to SQL.

    The strange thing is ,when I use Convert.ToInt32(rq.BORNDATE.Substring(0, 4)) instead of using Int32.Parse(rq.BORNDATE.Substring(0, 4)), it's running properly.

    Why?

    Great thanks !

    Friday, April 1, 2011 9:19 AM

Answers

  • Why not use convert if it is running properly?


    Edit:

    Sorry, the question was why :-) Methods have been coded so it generates sql instead of executing the code on top. This is the only way everything can be evaluated late.

    E.g. pretend you have a property called Lineprice on an orderline, and it was a multiply of quantity and itemprice, then if you took sum of that, then noone have told the sql generator that * in code, also becomes a * in sql.

    But if you instead wrote it out, like

    var query = from row in orderline select quantity*linePrice;

    return query.Sum(), then it would work, because then linq to sql know how to generate the sql:

    select sum(quantity*linePrice) from orderline

    • Marked as answer by S.Fen Friday, April 1, 2011 3:20 PM
    Friday, April 1, 2011 12:52 PM

All replies

  • Any ideas?
    Friday, April 1, 2011 12:48 PM
  • Why not use convert if it is running properly?


    Edit:

    Sorry, the question was why :-) Methods have been coded so it generates sql instead of executing the code on top. This is the only way everything can be evaluated late.

    E.g. pretend you have a property called Lineprice on an orderline, and it was a multiply of quantity and itemprice, then if you took sum of that, then noone have told the sql generator that * in code, also becomes a * in sql.

    But if you instead wrote it out, like

    var query = from row in orderline select quantity*linePrice;

    return query.Sum(), then it would work, because then linq to sql know how to generate the sql:

    select sum(quantity*linePrice) from orderline

    • Marked as answer by S.Fen Friday, April 1, 2011 3:20 PM
    Friday, April 1, 2011 12:52 PM
  • Think I got you.

    Thanks very much.

    Friday, April 1, 2011 3:21 PM