c# - How do I quickly create a new collection of objects from a collection of larger objects? -
i have 4 collections of objects:
public static concurrentbag<string> productcodes; public static concurrentdictionary<string, product> productdictionary; public static concurrentbag<iimim00> iimiml; // 37782 items public static concurrentbag<iimar00> iimarl; // 73516 items public static concurrentbag<productinf> additionall; // 6238 items public static concurrentbag<pf_cossto> productsandcosts; // 862096 items
so first of unique list of product codes want create new 'product' objects from:
parallel.foreach(productsandcosts, => productcodes.add(i.improd)); productcodes = new concurrentbag<string>(productcodes.distinct().tolist())
the product class:
public class product { public iimim00 iimim { get; set; } public list<iimar00> iimar { get; set; } public productinf productadditional { get; set; } }
my routine sort , create dictionary product code , product object:
parallel.foreach(productcodes, sortandcreate); public static void sortandcreate(string productcode) { var product = new product {iimim = iimiml.single(x => x.improd.equals(productcode))}; seek { product.iimar = iimarl.where(x => x.arprod.equals(productcode)).tolist(); } grab (exception) { product.iimar = new list<iimar00>(); } seek { product.productadditional = additionall.single(x => x.productcod.equals(productcode)); } grab (exception) { product.productadditional = new productinf(); } productdictionary.tryadd(productcode, product); }
the seek catches there because product object wont have instance of iimar00 or productinf.
the solution have come slow takes on 2:30 on i5. i'm not sure if there improve way around this.
you should never utilize try-catch
programme flow, because throwing handling exceptions takes much time. in fact it's covert if-else
. add together null check , you'll gain time lost exception handling:
product.iimar = iimarl.where(x => x.arprod != null && x.arprod.equals(productcode)) .tolist();
c# linq collections parallel-processing
No comments:
Post a Comment