var AddQuery = from newdist in newSpec.Distributions
where !( from oldDist in oSpec.Distributions
select oldDist.Id).Contains( newdist.Id )
select newdist;
foreach (var dist in AddQuery)
{
dist.operation="Add";
}
var updateQuery = from oldDistributionForUpdate in ospec.Distributions
where (from newDistributionForUpdate in newspec.Distributions
select newDistributionForUpdate.DistributionId).Contains(oldDistributionForUpdate.DistributionId)
&&
(from newDistributionForUpdate in newpaymentSpecification.Distributions
select newDistributionForUpdate.Amount).Equals(oldDistributionForUpdate.Amount)
select oldDistributionForUpdate;
foreach (var dist in updateDistQuery)
{
dist.operation = "Update";
}
I am planning to have a collection of objects from both the query results & process them, Is there a simpler way to achieve what I am doing?
In your original snippet, you are matching on Id
for AddQuery
and on DistributionId
for UpdateQuery
. If this is a typo and it should be Id
or DistributionId
for both cases, then you can process Add and Update much more efficiently in the same loop.
var AddUpdateQuery = from newdist in newSpec.Distributions
join oldDist in oSpec.Distributions
on newdist.Id equals oldDist.Id into matchingDistributions
select new { newdist, matchingDistributions };
foreach (var dist in AddUpdateQuery)
{
if (!dist.matchingDistributions.Any())
{
dist.newdist.operation = "Add";
//additional processing for "Add" operation.
}
else if (dist.newdist.Amount == dist.matchingDistributions.First().Amount)
{
dist.newdist.operation = "Update";
//additional processing for "Update" operation.
}
}
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.