Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Thursday, October 2, 2014

Cannot convert type 'System.Linq.IQueryable' to 'int'

You may have encountered Cannot convert type 'System.Linq.IQueryable' to 'int' compile time issue in Linq queries. Have a look on bellow example:


Because, Linq query is lazy, and executes only after you request the value. So, If you want to get exact value use FirstOrDefault() as shown below;
 private void GetUserInspectionReminderConfigurations(string relatedTable, int relatedID)
 {
    int codeID = (from c in _context.Codes
                  join h in _context.CodeHeaders on c.CodeHeaderID equals h.CodeHeaderID
                  where (h.CodeTable == "NotificationTypes" && c.CodeValue == "INSPECTIONALERT")
                  select c.CodeID).FirstOrDefault();

 }