LINQ将行的列值分隔为.net中的不同行

nag*_*nag 6 .net c# linq

考虑我有一个从oracle数据库检索的数据表,格式如下

SNo.  |      Product                      |  Cost
-------------------------------------------------
1     |      colgate,closeup,pepsodent    |  50
2     |      rin,surf                     |  100
Run Code Online (Sandbox Code Playgroud)

我需要使用linq.Need将其更改为以下格式,以通过保持其他列相同来使用逗号分隔产品列.

SNo.  |        Product      |   Cost
-------------------------------------
1     |        colgate      |    50
1     |        closeup      |    50
1     |        pepsodent    |    50
2     |        rin          |    100
2     |        surf         |    100
Run Code Online (Sandbox Code Playgroud)

Pio*_*yna 5

请试试这个:

List<Product> uncompressedList = compressedProducts
    .SelectMany(singleProduct => singleProduct.ProductName
                                    .Split(',')
                                    .Select(singleProductName => new Product 
                                    { 
                                        SNo = singleProduct.SNo, 
                                        ProductName = singleProductName, 
                                        Cost = singleProduct.Cost 
                                    }))
    .ToList();
Run Code Online (Sandbox Code Playgroud)

编辑:

产品类定义如下:

public class Product
{
    public Int32 SNo { get; set; }
    public String ProductName { get; set; }
    public Int32 Cost { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

compressedProducts只是您第一个示例中的初始产品列表。