Hi,
I have on table with few data in that i have one datetime variable called published date, i want to merge this column into one single column.
i know to merge string type column but how to merge column of datetime type.
sample input:
I’m using linq query to merge string type column : (From row In dtOriginal.AsEnumerable()
Group row By BookID = row(“BookID”) Into Group
Select dtOriginal.Clone.Rows.Add(BookID, String.Join(“,”,Group.Select(Function(r) r(“BookName”).ToString())))).CopyToDataTable()
How do I use linq query for datetime column as well?
(From row In dtOriginal.AsEnumerable()
Group row By BookID = row("BookID") Into Group
Select dtOriginal.Clone.Rows.Add(BookID,
String.Join(",", Group.Select(Function(r) r("BookName").ToString())),
String.Join(",", Group.Select(Function(r) Convert.ToDateTime(r("PublishesDate")).ToString("MM/dd/yyyy"))))
).CopyToDataTable()
(From row In dtOriginal.AsEnumerable()
Group row By BookID = row(“BookID”) Into Group
Select dtOriginal.Clone.Rows.Add(BookID,
String.Join(“,”, Group.Select(Function(r) r(“BookName”).ToString())),
String.Join(“,”, Group.Select(Function(r) DateTime.ParseExact(r(“PublishesDate”).ToString,“dd-MM-yyyy”, System.Globalization.CultureInfo.InvariantCulture).ToString(“dd-MM-yyyy”))), row(“Price”).ToString
).CopyToDataTable()
Hi @Yoichi, query is working fine when we have string type columns but in my case I have a datetime column “publishes date” and it contains some date and some null value I need a similar query which covers datetime column as well.
(From row In dtOriginal.AsEnumerable()
Group row By BookID = row("BookID") Into Group
Select dtOriginal.Clone.Rows.Add(
BookID,
String.Join(",", Group.Select(Function(r) r("BookName").ToString())),
String.Join(",", Group.Select(Function(r) If(IsDBNull(r("PublishesDate")), String.Empty, Convert.ToDateTime(r("PublishesDate")).ToString("MM/dd/yyyy"))))
)).CopyToDataTable()
dt.AsEnumerable.GroupBy(Function(r) r("BookID").ToString).Select(Function(g) dt.Clone.LoadDataRow({g.Key,String.Join(",",g.Where(Function(r) not String.IsNullOrEmpty(r("BookName").ToString)).Select(Function(r) r("BookName").ToString)),String.Join(",",g.Where(Function(r) not String.IsNullOrEmpty(r("PublishesDate").ToString)).Select(Function(r) CDate(r("PublishesDate").ToString).ToString("dd-MM-yyyy"))),String.Join(",",g.Where(Function(r) not String.IsNullOrEmpty(r("Price").ToString)).Select(Function(r) r("Price").ToString).Distinct())},False)).CopyToDataTable()