简体   繁体   English

使用 linq 迭代对象的最佳方法是什么

[英]What is the best way to iterate objects using linq

           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 .在您的原始代码段中,您正在匹配AddQueryIdUpdateQueryDistributionId 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.如果这是一个拼写错误并且这两种情况都应该是IdDistributionId ,那么您可以在同一个循环中更有效地处理 Add 和 Update。

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.
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用LINQ-to-SQL时,透明地记录对象更改的最佳方法是什么? - What is the best way to transparently log changes to objects when using LINQ-to-SQL? 使用LINQ为集合中的所有对象的属性赋值的最佳方法 - Best way to assign a value to a property of all objects in a collection using LINQ 使用LINQ从xml创建实体对象的最佳方法 - Best way to create entity objects from xml using LINQ 使用Linq在两个列表之间进行规范化的最佳方法是什么? - What's the best way to normalize between two lists using Linq? 使用 Linq 到 Datatable 显示日期时间的最佳方法是什么? - What is the best way to display date time using Linq to Datatable? 向后迭代SortedDictionary的最佳方法是什么? - What is the best way to iterate over a SortedDictionary backwards? 将此SQL查询转换为Linq的最佳方法是什么? - What is the best way to convert this SQL query to Linq? 什么是Sql Raw Queries或Linq的最佳方法 - What is Best way Sql Raw Queries or Linq LINQ中排序1000000条记录的最佳方法是什么 - What is the best way in LINQ to sort 1000000 Records 在Linq to NHibernate中测试状况的最佳方法是什么? - What is the best way to test a condition in Linq to NHibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM