0

I have the following piece of code which I am trying to improve:

var ApprovedAllowancesProductIds = from item in ApprovedAllowancesListFiltered select item.ProductId; //(1)

List<Product> productList = new List<Product>();
List<string> partitionProductKeys = new List<string>();

foreach (var item in ApprovedAllowancesProductIds)   //(2)
{
    string[] partitionKey = new string[] { CountryCD, item.ToString() };
    string PartitionKey = partitionKey.ConcatenatedStringWithPipeSeparator();
    partitionProductKeys.Add(PartitionKey);
}

var resultProduct = await _tableStorageService.FetchDataByMultiplePartitionKey<Product>(partitionProductKeys, QueryComparisonEnums.Equal);
productList.Add((Product)resultProduct);

For better performance I want to combine the functionality of (2) i.e., the entire foreach loop in (1). I saw some example as: Example1 and Example2 but they don't serve my purpose so please don't mark this as duplicate.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

you could combine with Select

var partitionProductKeys = ApprovedAllowancesProductIds
    .Select(item => 
    {
        string[] partitionKey = new string[] { CountryCD, item.ToString() };
        return partitionKey.ConcatenatedStringWithPipeSeparator();
    })
    .ToList();

you could apply the same concept with ApprovedAllowancesListFiltered if you want to merge the fist line as well with this code.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • Thanks vivek for quick response. However, Can we also do it while creating the ApprovedAllowancesProductIds i.e., at line var ApprovedAllowancesProductIds = from item in ApprovedAllowancesListFiltered select item.ProductId; ??. I would save me an additional variable cost. – Abhishek_Singh_Rana Dec 27 '22 at 11:33
  • var ApprovedAllowancesProductIds = from item in ApprovedAllowancesListFiltered select item.ProductId.ToString() .ToList().Select(item => { string[] partitionKey = new string[] { CountryCD, item.ToString() }; return partitionKey.ConcatenatedStringWithPipeSeparator(); }).ToList(); – Abhishek_Singh_Rana Dec 27 '22 at 11:37
  • @Abhishek_Singh_Rana yes yo can, just give it a try.I have provided the direction – Vivek Nuna Dec 27 '22 at 11:42
  • Thanks :)..I will update the final answer when I test :) – Abhishek_Singh_Rana Dec 27 '22 at 11:43
0

I suggest you remove the outer list completely and loop on the objects instead of the property ProductId of the objects:

List<Product> productList = new List<Product>();
List<string> partitionProductKeys = new List<string>();
foreach (var item in ApprovedAllowancesListFiltered)
{
    string[] partitionKey = new string[] { CountryCD, item.ProductId.ToString() };
    string PartitionKey = partitionKey.ConcatenatedStringWithPipeSeparator();
    partitionProductKeys.Add(PartitionKey);
}
var resultProduct = await _tableStorageService.FetchDataByMultiplePartitionKey<Product>(partitionProductKeys, QueryComparisonEnums.Equal);
productList.Add((Product)resultProduct);

I hope this helps.

user3505708
  • 64
  • 2
  • 12