2011-08-16 1 views
0
  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"; 
      } 

를 사용하여 개체를 반복하는 가장 좋은 방법은 무엇입니까?나는 & 과정들을, 내가 뭐하는 거지 달성하는 간단한 방법이 있나요 결과 쿼리 모두에서 개체의 컬렉션을 가질 계획입니다 LINQ

+0

나는 컬렉션을 반복하는 방법이 가장 쉬운 방법이라고 생각합니다. AddQuery.ToList()를 사용할 수 있습니다. ForEach (i => i.operation = "Add")' –

답변

0

원래 스 니펫에서 AddQueryId이고 UpdateQueryDistributionId입니다. 이것이 오타이고 두 경우 모두 Id 또는 DistributionId이어야하는 경우 동일한 루프에서 훨씬 효율적으로 추가 및 업데이트를 처리 할 수 ​​있습니다.

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. 
     } 
    } 
관련 문제