System.Linq.Enumerable.WhereListIterator 和 System.Linq.Enumerable.WhereListIterator 之间有什么区别? System.Linq.Enumerable.WhereSelectListIterator?

发布于 2024-12-11 12:37:23 字数 2477 浏览 0 评论 0原文

System.Linq.Enumerable.WhereListIterator 和 System.Linq.Enumerable.WhereListIterator 之间有什么区别? System.Linq.Enumerable.WhereSelectListIterator?

我注意到的一个区别是 TypeWhereListIterator 反映了集合对象的更改,但WhereSelectListIterator 没有,

我会让它更清楚,例如。

我有一个场景,从存储库中获取域对象

var buckets = testRepository.GetBuckets(testIds);

,然后在循环内从上述集合中选择某些存储桶

 var bucketsForTest = buckets.Where(bucket => bucket.TestID == test.testId);

,然后在 LooserTrafficDisributor 对象的方法内更改所有存储桶对象的单个属性。

ITrafficDistributor distributor = new LooserTrafficDisributor(bucketsForTest);

IEnumerable<Bucket> updatedBuckets = distributor.Distribute(test.AutoDecision);

LooserTrafficDisributor 的构造函数

  public LooserTrafficDisributor(IEnumerable<Bucket> allBuckets)
    {
        this.allBuckets = allBuckets;
    }

LooserTrafficDistributor 中的分发方法如下所示

 private IEnumerable<Bucket> DistributeTraffic(bool autoDecision)
 {
  // allBuckets is class variable in LooserTrafficDistributor object which is set through constructor shown above .
  // Omitted other details

                allBuckets.Where(bucket=> bucket.IsControl == false).ToList()
                    .ForEach(bucket => bucket.TrafficPercentage += 10 ));
return allBuckets
 }

此后我可以看到 IEnumerable UpdatedBuckets 集合中反映的更改。

但如果我这样做,即不是从存储库中获取 Bucket 集合,而是执行 select & 操作。然后以类似的方式更新所有 Bucket 对象,如下所示,

 var bucketsForTest = testRows.Where(testrow => testrow.url == url.url).Select(currRow => new Bucket
    {
                TestID = currRow.TestId,
                    BucketID = currRow.BucketId,
                    BucketName = currRow.c_bucket_name,
                    TrafficPercentage = Convert.ToInt32(currRow.i_bucket_percentage),
                    IsControl = currRow.b_is_control,
                    IsEnabled = currRow.b_enabled,
                    UpdatedAdminId = currRow.i_updated_admin_id,
                    LogAsSection = currRow.i_log_as_section
             }) ;

  ITrafficDistributor distributor = new LooserTrafficDisributor(bucketsForTest);

  IEnumerable<Bucket> updatedBuckets = distributor.Distribute(test.AutoDecision, strategy.GetStatisticallySignificantLoosingBucketIds());

我无法在 IEnumerable UpdatedBuckets 集合中反映更改。 事实上,我在 DistributeTraffic 方法内部进行了调试,即使在每次循环后都没有反映更改。

What is difference between System.Linq.Enumerable.WhereListIterator & System.Linq.Enumerable.WhereSelectListIterator?

One difference I hav noticed is Type WhereListIterator reflects changes on collection object but WhereSelectListIterator does not

I will make it more clear for eg.

I hav a scenario where I fetch my Domain Object from Repository

var buckets = testRepository.GetBuckets(testIds);

Then I select certain buckets from the above collection inside a loop

 var bucketsForTest = buckets.Where(bucket => bucket.TestID == test.testId);

Then I change a single property of all the Bucket Objects inside the method of LooserTrafficDisributor object.

ITrafficDistributor distributor = new LooserTrafficDisributor(bucketsForTest);

IEnumerable<Bucket> updatedBuckets = distributor.Distribute(test.AutoDecision);

Constructor of LooserTrafficDisributor

  public LooserTrafficDisributor(IEnumerable<Bucket> allBuckets)
    {
        this.allBuckets = allBuckets;
    }

The distribute method inside LooserTrafficDistributor looks like this

 private IEnumerable<Bucket> DistributeTraffic(bool autoDecision)
 {
  // allBuckets is class variable in LooserTrafficDistributor object which is set through constructor shown above .
  // Omitted other details

                allBuckets.Where(bucket=> bucket.IsControl == false).ToList()
                    .ForEach(bucket => bucket.TrafficPercentage += 10 ));
return allBuckets
 }

After this I can see the reflected changes inside the IEnumerable updatedBuckets collection.

But if I do this i.e. instead of fetching Bucket collection from repository do a select & then Update all the Bucket objects in similar manner as follows

 var bucketsForTest = testRows.Where(testrow => testrow.url == url.url).Select(currRow => new Bucket
    {
                TestID = currRow.TestId,
                    BucketID = currRow.BucketId,
                    BucketName = currRow.c_bucket_name,
                    TrafficPercentage = Convert.ToInt32(currRow.i_bucket_percentage),
                    IsControl = currRow.b_is_control,
                    IsEnabled = currRow.b_enabled,
                    UpdatedAdminId = currRow.i_updated_admin_id,
                    LogAsSection = currRow.i_log_as_section
             }) ;

  ITrafficDistributor distributor = new LooserTrafficDisributor(bucketsForTest);

  IEnumerable<Bucket> updatedBuckets = distributor.Distribute(test.AutoDecision, strategy.GetStatisticallySignificantLoosingBucketIds());

I can't get the changes reflected inside the IEnumerable updatedBuckets collection.
Infact I debugged inside the DistributeTraffic methods even there the changes were not reflected after each loop round.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名的好友 2024-12-18 12:37:23

.Where() 使您的项目成为 IEnumerable,其中包含满足 where 条件的所有元素。如果您对该结果集运行 .Select(),您将获得在 select 语句中创建的新元素的 IEnumerable。因此,对原始元素的更改将不会反映在新元素上。

在您的示例中,您为原始列表中满足您的位置条件的每个存储桶创建一个新的存储桶对象,将内容从原始存储桶复制到新存储桶。

.Where() makes an IEnumerable of your items containing all elements which fullfil the where criteria. If you run a .Select() on that result set, you will get a IEnumerable of new elements you've created in the select-statement. So changes to the original elements will not reflect on the new elements.

In your example you create for every Bucket in the original list fullfilling your where criteria a new Bucket object, copying the content from the original bucket to the new Bucket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文