Reputation: 29
select * from Employees where DataofJoin ='2005-01-01 00:00:00.000'
I wrote this Linq Query as
public JsonResult Dif()
{
var ss = Convert.ToDateTime("2001-01-01 00:00:00.000");
var x = (from n in db.Employees
where n.DataofBirth == ss
select n).First();
return new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
But its throwing the following error:
"An exception of type 'System.InvalidOperationException' occurred in System.Core.dll
but was not handled in user code"
Upvotes: 0
Views: 312
Reputation: 3319
As I can Understand , This issue is because of attempt to get an item from empty object.
Because you are making a call of .First()
and there is an empty result returned by the query. So if you do obj.First()
of an empty object it throws exception. Use obj.FirstOrDefault()
To avoid the exception.
And if Linq is not returning the data is an issue then use Sql Profiler to check what query is been called,or change the date filter accordingly.
To avoid error Use -
var x = (from n in db.Employees
where n.DataofBirth == ss
select n).FirstOrDefault();
Update
For getting proper date, do something like this .
var ss = new DateTime (2005,01,01,0,0,0); //use this date in linq query
Hope it will help you.
Upvotes: 2