Hi,
I have this code where on the filed "UnitPrice" i try to retrieve a value from a table.
code:
var destinationRecord = new DepartmentCopy
{
DepartmentCopyId = sourceRecord.DepartmentId,
Name = sourceRecord.Name,
GroupName = sourceRecord.GroupName,
ModifiedDate = sourceRecord.ModifiedDate,
PurchaseOrderDetailId = sourceRecord.PurchaseOrderDetailId,
------ error here -----------------------
UnitPrice = _dbContext.GetUnitPrice(sourceRecord.PurchaseOrderDetailId),
--------------------------------------------
};
the field PurchaseOrderDetailId is an integer and so the code where i try to retrieve the value:
code:
public PurchaseOrderDetail? GetUnitPrice(int id)
{
return (from o in PurchaseOrderDetails
where o.PurchaseOrderDetailId == id
select o).FirstOrDefault();
}
but the compiler go in error with the argument : cannot convert from int? to int
..where i'm wrong?
thks to everyone

Jignesh KumarRank #91 · 22.9K reputationPosted Jun 13, 2023, 3:59 AM EDT
please use below one,
UnitPrice = sourceRecord.PurchaseOrderDetailId.HasValue ? _dbContext.GetUnitPrice(sourceRecord.PurchaseOrderDetailId.Value) : 0,Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 12, 2023, 2:29 PM EDT
public PurchaseOrderDetail? GetUnitPrice(int? id) { return (from o in PurchaseOrderDetails where o.PurchaseOrderDetailId == id select o).FirstOrDefault(); }Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 12, 2023, 10:47 PM EDT
Rajkiran Swain's solution would work.
Mic Got91 reputationPosted Jun 13, 2023, 5:44 AM EDT
PurchaseOrderDetail purchaseOrderDetail = _dbContext.GetUnitPrice(sourceRecord.PurchaseOrderDetailId); if (purchaseOrderDetail != null) { destinationRecord.UnitPrice = purchaseOrderDetail.UnitPrice; }Solved with the code below, as suggested from the compiler.