Hi Sir,
I am getting the below error:
"Object with the same key already exists in the Objectstatemanager. The existing object is in the modified state.
it goes inside the foreach statement and inserting one row into the table. When it goes for the second time to insert i am getting that error.
The below is the code:
var data = (from mp in mpciPolicyRepository.GetAll()
join wfrp_mc in mpciCoverageRepository.GetAll() on mp.OID equals wfrp_mc.MpciPolicyOID
join wfrp in wfrpFarmReportsSubDetailsRepository.GetAll() on wfrp_mc.PreviousOID equals wfrp.MpciCoverageOID
join rel_mc_old in mpciCoverageRepository.GetAll() on wfrp.RelatedMpciCoverageOID equals rel_mc_old.OID
join rel_mc_new in mpciCoverageRepository.GetAll() on rel_mc_old.OID equals rel_mc_new.PreviousOID
join wfrp_ex in wfrpFarmReportsSubDetailsRepository.GetAll()
on new { a=wfrp_mc.OID, b=(long?)rel_mc_new.OID } equals new { a=wfrp_ex.MpciCoverageOID, b=wfrp_ex.RelatedMpciCoverageOID}
into wfrpcollection from subwfrp in wfrpcollection.DefaultIfEmpty()
where mp.ReinsuranceYear == reinsuranceYear && wfrp_mc.CommodityCode == "0076" && mp.PolicyNumber=="1057385" &&
!MpciCommodityStatusCodeExtensions.CancelledMpciCommodityStatusCodes.Contains(wfrp_mc.CommodityStatus ?? 0)
&& wfrp_mc.HasRelatedCoverage == 1 && wfrp.RelatedMpciCoverageOID != null
&& !MpciCommodityStatusCodeExtensions.CancelledMpciCommodityStatusCodes.Contains(rel_mc_old.CommodityStatus ?? 0)
&& !MpciCommodityStatusCodeExtensions.CancelledMpciCommodityStatusCodes.Contains(rel_mc_new.CommodityStatus ?? 0)
//&& rel_mc_old.CommodityStatus < '7' && rel_mc_new.CommodityStatus < '7'
&& subwfrp == null
select new WfrpFarmReportsSubDetailViewModel
{
MpciCoverageOID = wfrp_mc.OID,
RelatedMpciCoverageOID = rel_mc_new.OID,
AssociatedAipCode = wfrp.AssociatedAipCode,
AssociatedAipPolicyNumber = wfrp.AssociatedAipPolicyNumber,
AssociatedAipLocationStateCode = wfrp.AssociatedAipLocationStateCode,
AssociatedAipLocationCountyCode = wfrp.AssociatedAipLocationCountyCode,
AssociatedAipCommodityCode = wfrp.AssociatedAipCommodityCode,
WrittenAgreement = wfrp.WrittenAgreement,
}).ToList();
//on new { wfrp_ex.MpciCoverageOID, wfrp_ex.RelatedMpciCoverageOID } equals new { wfrp_mc.OID , rel_mc_new.OID }
if (data.Count != 0 || data != null)
{
foreach (var subdetail in data)
{
subDetails.MpciCoverageOID = subdetail.MpciCoverageOID;
subDetails.RelatedMpciCoverageOID = subdetail.RelatedMpciCoverageOID;
subDetails.AssociatedAipCode = subdetail.AssociatedAipCode;
subDetails.AssociatedAipPolicyNumber = subdetail.AssociatedAipPolicyNumber;
subDetails.AssociatedAipLocationStateCode = subdetail.AssociatedAipLocationStateCode;
subDetails.AssociatedAipLocationCountyCode = subdetail.AssociatedAipLocationCountyCode;
subDetails.AssociatedAipCommodityCode = subdetail.AssociatedAipCommodityCode;
subDetails.WrittenAgreement = subdetail.WrittenAgreement;
wfrpFarmReportsSubDetailsRepository.Add(subDetails);
}
wfrpFarmReportsSubDetailsRepository.Save();
}
Can you provide some solution on the above code.
Thanks