LINQ到一排列值分开,在.net中不同行 [英] LINQ to separate column value of a row to different rows in .net

查看:149
本文介绍了LINQ到一排列值分开,在.net中不同行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据表,从Oracle数据库中按以下格式检索

 斯诺。 |产品|成本
-------------------------------------------------
1 |高露洁,特写镜头,pepsodent | 50
2 | RIN,冲浪| 100
 

我需要使用linq.Need通过保持其它列相同,分离与逗号的帮助产物塔来改变这种成以下格式

 斯诺。 |产品|成本
-------------------------------------
1 |高露洁| 50
1 |特写镜头| 50
1 | pepsodent | 50
2 | RIN | 100
2 |冲浪| 100
 

解决方案

请试试这个:

 名单,其中,产品> uncom pressedList = com的pressedProducts
    .SelectMany(singleProduct => singleProduct.ProductName
                                    。分裂(',')
                                    。选择(singleProductName =>新建商品
                                    {
                                        SNO = singleProduct.SNo,
                                        产品名称= singleProductName,
                                        成本= singleProduct.Cost
                                    }))
    .ToList();
 

编辑:

产品类的定义如下:

 公共类产品
{
    公众的Int32 SNO {获得;组; }
    公共字符串产品名称{获得;组; }
    公众的Int32成本{获得;组; }
}
 

COM pressedProducts 的是从你的第一个例子中的产品仅仅是初步名单。

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

解决方案

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.

这篇关于LINQ到一排列值分开,在.net中不同行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆