string sStoreStockFeed = "";
string sSeparator = "";
var distinctStoreIDList = skuStoreStockLevels.Select(x => x.Item1).Distinct();
foreach (var storeID in distinctStoreIDList)
{
foreach (var item in skuStoreStockLevels)
{
if (item.Item1 == storeID)
{
// add this one to a job for this store
sStoreStockFeed += sSeparator + item.Item1.ToString() + "," + item.Item2.ToString() + "," + item.Item3.ToString();
sSeparator = "|";
}
}
// some code to process the string before moving on
sStoreStockFeed = "";
sSeparator = "";
}
在上面的代码片段中,skuStoreStockLevels 恰好是一个 Tuple 类型的 List,而 Item1 是 StoreID。得到一个不同的列表后,它会遍历(非不同的)列表以获取每个适用的项目。效率低下是(大)内部列表为每个不同的项目(StoreID)重复迭代。