2

I am trying to take number of variables from my database with a where condition but I get an error like

'LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method'.

Here is my code:

var tickets = (from a in user.TicketTable where a.HandlerId == int.Parse(Session["emp"].ToString()) && a.Done == true select a.TicketId);
int count = 0;
foreach (var n in tickets) 
{
 count++;
}
lblDone.Text = count.ToString();

I tried other methods to convert my session to int but they all gave the same result.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Koray Durudogan
  • 624
  • 4
  • 12
  • 31
  • 1
    Sidenote: if you stored that "emp" value as int, then you just need to cast Session["emp"] to int. No need to first convert to string only to convert it back to int. – Hans Kesting Nov 15 '15 at 15:17

2 Answers2

1

int.Parse couldn't be translated to T-SQL, Linq to Entities couldn't recognize it. You can modify the code as below (Move the parse function out of expression):

int emp = int.Parse(Session["emp"].ToString());

var tickets = (from a in user.TicketTable where a.HandlerId == emp &&
                a.Done == true 
                select a.TicketId);

Also Have a look at this link may useful : How to resolve: LINQ to Entities does not recognize the method int.Parse.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

you need to move the parse outside of the Linq to Entities query, e.g.

var emp =int.Parse(Session["emp"].ToString()) ;
var tickets = (from a in user.TicketTable where a.HandlerId == emp &&
 a.Done == true select a.TicketId);
NDJ
  • 5,189
  • 1
  • 18
  • 27