Click here to Skip to main content
15,860,861 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I am using LINQ query to check ID(eventTypeID) from EventType table.
This is the query which I have written,while debugging it will throw the exception in this line,as 'specified cast is not valid'.

C#
EventType events = db.EventType.Single(f => f.eventTypeID == eventTypeID);


Does any one know why it is so??

Regards
Madhu
Posted
Updated 21-Dec-10 23:48pm
v2

Hi Madhu,

Please Try This

EventType events = (from f in db.EventType where f.eventTypeID == eventTypeID select f).Single();
 
Share this answer
 
v2
Try using the following (SingleOrDefault() method instead of Single() method):

EventType events = db.EventType.SingleOrDefault(f => f.eventTypeID == eventTypeID);
 
Share this answer
 
Comments
madhusudhan.k 22-Dec-10 6:00am    
Hi Al-Farooque Shubho,
thanks for ur post. But still it is showing the same exception.
When I select the line & go to quick watch means it shows the value as 'Expression cannot contains lambda expressions'.

Regards
Madhu
Dalek Dave 23-Dec-10 20:19pm    
Good Answer.
Try this:

C#
EventType events = (EventType)(db.EventType.SingleOrDefault(f => f.eventTypeID == eventTypeID));


 
Share this answer
 
v2
Comments
madhusudhan.k 22-Dec-10 6:12am    
Hi John Simmons,
Still it is throwing the same exception.
When I select the line & go to quick watch means it shows the value as 'Expression cannot contains lambda expressions'.

Regards
Madhu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900