Noticed a strange thing the other day, a list has the property ItemCount and also an Items collection which has its own .Count b

本文通过一个具体的代码示例展示了在使用 SharePoint 列表时,ItemCount 和 Items.Count 属性值不一致的问题。此现象提示开发者在操作列表时需注意内部集合更新的不同步情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Noticed a strange thing the other day, a list has the property ItemCount and also an Items collection which has its own .Count but they don't always give the same answer. If I have a piece of code like this pointing to a list with 4 items in it.

1  using  (SPSite site  =   new  SPSite( " http://mwmoss/marktest " ))
2  {
3  using  (SPWeb web  =  site.OpenWeb())
4  {
5  SPList list  =  web.Lists[ " Mark's Contacts " ];
6 
7  while (list.Items.Count  >   0 )
8  {
9  list.Items[ 0 ].Delete();
10  Console.WriteLine( " ItemCount  "   +  list.ItemCount.ToString());
11  Console.WriteLine( " Items.Count  "   + list.Items.Count.ToString());
12  }
13  }
14  }


The output looks like this:-

ItemCount 4
Items.Count 3
ItemCount 4
Items.Count 2
ItemCount 4
Items.Count 1
ItemCount 4
Items.Count 0

I guess internally some collection isn't getting updated as we go round the loop. Something to be careful of anyway.

引用:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值