2

I tried to find what causes the issue

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

The error happens when the execution hits the if condition. The line if (query.Any()) throws the exception and I couldn't figure why. Can you spot the problem?

My query:

var query = from clt in vclt.cltpar
            join p in vclt.profession //left join
                 on clt.no_prof equals p.no_prof
                 into temp
            from prof in temp.DefaultIfEmpty()
            where
              clt.no_pays == short.Parse(no_pays) &&
              clt.tp_pid == tp_pid &&
              clt.no_pid == no_pid &&
              clt.no_seq == short.Parse(no_seq)
            select new
            {
                clt.no_pays,
                clt.tp_pid,
                clt.no_pid,
                clt.no_seq,
                dt_cre = clt.dat_nais,
                DesigClt = vclt.get_nompren(clt.no_pays, clt.tp_pid, clt.no_pid, clt.no_seq).ToString(),
                AdrClt = prtf.adresseclt(clt.no_pays, clt.tp_pid, clt.no_pid, clt.no_seq).ToString(),
                CodRisque = vclt.get_coderisque(clt.no_pays, clt.tp_pid, clt.no_pid, clt.no_seq).ToString(),
                FormeJur = "",
                Secteur = prof.lib_prof,
                Registre = "",
                MatFisc = "",
                NumTeleph = prtf.get_numtel(clt.no_pays, clt.tp_pid, clt.no_pid, clt.no_seq).ToString(),
                DateDebRel = ogcc.get_datdebrel(clt.no_pays, clt.tp_pid, clt.no_pid, clt.no_seq).ToString(),
                RNE = "",
                cd_dou = "0"
            };

if (query.Any())
{
     stuff here
}

Update I did as told i converted outside the query as follow

short n_pays = short.Parse(no_pays.ToString());
short n_seq = short.Parse(no_seq.ToString());

BUT this throws an exception that couldn't be resolved even when i used ToString()

Sleepy
  • 180
  • 3
  • 15

2 Answers2

2

Move the parsing of the parameters no_pays and no_seq outside the query. EF and LINQ2SQL can not convert all functions/expression to SQL code and thus the query is failing. It fails at query.Any() because at this line the query is converted to SQL and sent to the SQL server.

var nopays = short.Parse(no_pays);
var noseq = short.Parse(no_seq);
var query = from clt in vclt.cltpar
                        join p in vclt.profession //left join
                            on clt.no_prof equals p.no_prof
                            into temp
                        from prof in temp.DefaultIfEmpty()
                        where
                          clt.no_pays == nopays &&
                          clt.tp_pid == tp_pid &&
                          clt.no_pid == no_pid &&
                          clt.no_seq == noseq
                          ...
keenthinker
  • 7,645
  • 2
  • 35
  • 45
2

This behavior is by design. You are trying to embed a .NET library function to your EF query. As it is not translatable to SQL, this is not supported.

You must rewrite your query, without using the .Parse().

This particular case it may not too hard. Convert the string to short outside of the LINQ query

g.pickardou
  • 32,346
  • 36
  • 123
  • 268