在具有特定条件的DataTable中连接行

时间:2014-05-14 06:37:23

标签: c# datatable

我有像这样的DataTable dtTexts

varenummer   text                   ID   Brand    LineNumber
 100         sometext-name          0     nike     0
 100         sometext -longdesc     2     nike     2
 100         sometext-shortdesc     1     nike     0
 100         sometext -longdesc-2   2      nike    0

我正在将此转换为另一个像这样的数据表

  var myRows = from myRow in dtTexts.AsEnumerable()
                         where myRow.Field<string>("Varenummer") == "100"
                         select myRow;
  DataRow newRow = dtFinalData.NewRow();
  bool firstRow = true;
            foreach (var drText in myRows)
            {
                if (firstRow)
                {
                    firstRow = false;
                    newRow["Varenummer"] = drText.ItemArray[0];
                    newRow["Brand"] = drText.ItemArray[3];

                }

                if (drText.ItemArray[2].ToString() == "0")
                {
                    newRow["Varenavn"] = drText.ItemArray[1].ToString();
                }

                if (drText.ItemArray[2].ToString() == "1")
                {
                    newRow["ShortDescription"] = Regex.Replace(drText.ItemArray[1].ToString(), "<.+?>", string.Empty);

                }
                if (drText.ItemArray[2].ToString() == "2")
                {
                    newRow["LongDescription"] = Regex.Replace(drText.ItemArray[1].ToString(), "<.+?>", string.Empty);

                }

            }
            dtFinalData.Rows.Add(newRow);

获得这样的输出

varenummer brand   varenavn          shortdescription     longDescription
100         nike   sometext-name      sometext-shortdesc   sometext -longdesc 

现在问题是我的第一个dataTable中有两行Long描述。我想在linenumber列的基础上将它们连接起来。所以我得到了像

一样的输出。
 varenummer brand   varenavn          shortdescription     longDescription
    100     nike    sometext-name      sometext-shortdesc   sometext -longdesc+ sometext -longdesc-2

我怎样才能在C#中做到这一点?

1 个答案:

答案 0 :(得分:1)

不是覆盖newRow["LongDescription"],而是连接值:

if (drText.ItemArray[2].ToString() == "2")
{
    newRow["LongDescription"] += Regex.Replace(drText.ItemArray[1].ToString(), "<.+?>", string.Empty);
}

或者,如果你想要某种分离器:

if (drText.ItemArray[2].ToString() == "2")
{
    newRow["LongDescription"] = newRow["LongDescription"] +
                                (String.IsNullOrWhiteSpace(r.Field<string>(4)) ? "" : "\n") + 
                                Regex.Replace(drText.ItemArray[1].ToString(), "<.+?>", string.Empty);
}

或者,因为您已经在使用Linq:

// assuming there's always a single row with ID == 0 and one with ID == 1
var result = dtTextslike.AsEnumerable()
               .GroupBy(row => row["varenummer"])
               .Select(grp => new object[]
                {
                    grp.Key,
                    grp.First()["brand"],
                    grp.Single(r => r.Field<int>("ID") == 0)["text"],
                    grp.Single(r => r.Field<int>("ID") == 1)["text"],
                    String.Join("\n", grp.Where(r => r.Field<int>("ID") == 2)
                                         .OrderByDescending(r => r.Field<int>("LineNumber"))
                                         .Select(r => r.Field<string>("text")))
                });

foreach(var line in result)
    dtFinal.Rows.Add(line);