Question

I have a DataTable dtTexts like this

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

I am converting this to another datatable like this

  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);

and getting an output like this

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

Now the problem is I have two lines of Long description in my first dataTable. I want to concatenate them on the basis of linenumber column.So that I get an out put like

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

How can I do this in C#?

Was it helpful?

Solution

Instead of overwriting newRow["LongDescription"], just concatenate the values:

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

Or, if you want some kind of seperator:

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);
}

Or, since you're already using 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top