When a ClaimDetail is added to tmpClaim, this ClaimDetail is also added to claim. Can someone explain me why this occurs?
List<Claim> claims; // list data
foreach ( Claim claim in claims) {
Claim tmpClaim = datacontext.Claims.FirstOrDefault ( c => c.Id == claim.Id );
ClaimDetail claimDetail = new ClaimDetail ( );
claimDetail.ClaimDetailType = "Type";
// add claim
datacontext.Claims.InsertOnSubmit ( tmpClaim );
datacontext.SubmitChanges ( );
}
tmpClaim
isn't a new object. It's still a reference back to an element within claims
.
What this essentially means is that whatever you do to tmpClaim
is also happening with claims
. They are the same thing.
If you don't want changes to tmpClaim
to affect claims
, you'll need to create a new Claim
(as well as any copying/cloning that is necessary) and do all of your work on that new object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.