LINQ to separate column value of a row to different rows in .net
.net, c#, linq
Solution
Please try this:
List<Product> uncompressedList = compressedProducts
.SelectMany(singleProduct => singleProduct.ProductName
.Split(',')
.Select(singleProductName => new Product
{
SNo = singleProduct.SNo,
ProductName = singleProductName,
Cost = singleProduct.Cost
}))
.ToList();
EDIT:
Product class is defined as follows:
public class Product
{
public Int32 SNo { get; set; }
public String ProductName { get; set; }
public Int32 Cost { get; set; }
}
and compressedProducts is just the initial list of products from your first example.
Problem
Consider i have a datatable retrieved from oracle database in the following format ``` SNo. | Product | Cost ------------------------------------------------- 1 | colgate,closeup,pepsodent | 50 2 | rin,surf | 100 ``` I need to change this into the following format using linq.Need to separate the product column with the help of comma by keeping the other columns same. ``` SNo. | Product | Cost ------------------------------------- 1 | colgate | 50 1 | closeup | 50 1 | pepsodent | 50 2 | rin | 100 2 | surf | 100 ```