我的 .net 8.0 控制台应用程序中有以下代码,用于从 SharePoint 在线列表中检索所有项目:-
// get all items from Spare Parts Individula list
var sparePartIndividualList = context.Web.Lists.GetByTitle("Spare Part Individuals", p => p.Title,
p => p.Fields.QueryProperties(p => p.InternalName,
p => p.FieldTypeKind,
p => p.TypeAsString,
p => p.Title));
string viewXml2_1 = @"<View Scope='RecursiveAll'>
<ViewFields>
<FieldRef Name='Title' />
<FieldRef Name='SparePartStatus' />
<FieldRef Name='SparePartID' />
</ViewFields>
<Query>
</Query>
<RowLimit Paged='TRUE'>5000</RowLimit>
</View>";
// Load all the needed data using paged requests
bool paging2_1 = true;
string nextPage2_1 = null;
while (paging2_1)
{
var output = await sparePartIndividualList.LoadListDataAsStreamAsync(new RenderListDataOptions()
{
ViewXml = viewXml2_1,
RenderOptions = RenderListDataOptionsFlags.ListData,
Paging = nextPage ?? null,
}).ConfigureAwait(false);
if (output.ContainsKey("NextHref"))
{
nextPage2_1 = output["NextHref"].ToString().Substring(1);
}
else
{
paging2_1 = false;
}
}
该项目有 6 种不同的状态如下:-
那么构建包含状态和每个状态的项目总数的字典的最有效方法是什么?我不知道该怎么做 6 个单独的 Count(),因为这不会很有效。
我不明白为什么您认为创建 5-10 个实例来满足您的需要是一种“浪费”。这是做生意的成本。您使用的计算机不是 256k 内存。
创建一个代表每个单独状态的模型并将其插入字典中。public class StatusItem
{
public string Name { get; set; }
public int Count { get; set; }
...
}
然后将其添加到您的词典中:
var myDictionary = new Dictionary<string, StatusItem>();
var item = new StatusItem() { Name = "Open", Count = 1 };
myDictionary.Add(item.name, item);
....
Console.Writeline(myDictionary["Open"].Count);
完成工作即可。如果您遇到数以万计的记录,那么请考虑优化。