Skip to main content
1 of 6
Phantom
  • 443
  • 1
  • 6
  • 16

LINQ - Cannot convert from ' int? ' to ' int ', same variable name in different class but one is nullable

I have the following classes which have the same variable, but one of them is nullable, the other one is not because it is a primary key in that table:

[DataServiceKey("OrderID")] public class Order { public int OrderID { get; set; } public int? EmployeeID { get; set; } // ...other properties... // ... public Employee Employees { get; set; } } 
[DataServiceKey("EmployeeID")] public class Employee { public int EmployeeID { get; set; } // ...other properties... // ... public IEnumerable<Order> Orders { get; set; } } 

Now I am trying to create an association between Order class and Employee class, and this is how I did:

 // Create an association between Orders and Employees var _es = _Employees.ToDictionary(e => e.EmployeeID); var _os = _Orders.ToLookup(o => o.EmployeeID); foreach (var o in _Orders) o.Employees = _es[o.EmployeeID]; foreach (var e in _Employees) e.Orders = _os[e.EmployeeID]; 

I got cannot convert from "int?" to "int" error in _es[o.EmployeeID], I have tried every solutions provided in here, but none of them are work for me...I have also tried to use _es[o.EmployeeID.Value], it does solve the error but this time it gaves me System.InvalidOperationException error when I run it. Is there any possible solution will be applied to my issue ?

Phantom
  • 443
  • 1
  • 6
  • 16