I would prefer a simple foreach because it is known than LINQ is slower than the usual loop instructions, but if you really want to go this route, you can use something like what may be easier for you to read:
Considering
class ProductOracle { public int SNo { get; set; } public string Product { get; set; } public decimal Cost { get; set; } } class ProductEntity { public int SNo { get; set; } public string Product { get; set; } public decimal Cost { get; set; } }
Execution
var entities = new List<ProductOracle> { new ProductOracle{SNo=1,Product="colgate,closeup,pepsodent", Cost=50}, new ProductOracle{SNo=2,Product="rin,surf", Cost=100} };
WITH
var products = new List<ProductEntity>(); entities.ForEach(element => { element.Product.Split(',').ToList().ForEach(product => { products.Add(new ProductEntity { SNo = element.SNo, Product = product, Cost = element.Cost }); }); });
or
var products = entities.SelectMany(element => { var ProductEntities = new List<ProductEntity>(); element.Product.Split(',').ToList().ForEach(product => { ProductEntities.Add(new ProductEntity { SNo = element.SNo, Product = product, Cost = element.Cost }); }); return ProductEntities; });
Marco source share