Search code examples
c#datatableconcatenationdatarow

C# Concat DataTable Columns datatable as enumerable


What I am trying to do is concate two string columns and use them as one. I have seen many questions and answers on that but didn't really fix my problem.

I have a data table like the below which has 200 + rows and lots of columns. What I aim to do is to have "BVM2022000000810"

Bill_ID Serial Number
1 BVM 2022000000810
2 BVH 2022000000845

Here is my code structure and what I tried

                    var Table_bill= Table.AsEnumerable()
                    .Where(r => r["Bill_ID  "] != DBNull.Value && r["Bill_ID  "] != null)
                    .GroupBy(r => new { Bill_id= r["Bill_ID  "] })
                    .Select(r => new
                    {
                        Bill_id= r.Key.Bill_id.ToString(),
                        Count = r.Count(),
                        Bill_No= r.Select(x => x["Serial"]).Concat(r.Select(x => x["Number"])),
                        //Bill_No= string.Format("{0}{1}",r.Select(x =>x["Serial"].ToString()),r.Select(x=>x["Number"]).ToString()),
 });

Thanks by now!


Solution

  • can you try this code:

    Bill_No= string.Join("",r.Select(x => x["Serial"])) + string.Join("", r.Select(x => x["Number"]))

    or this:

    Bill_No= string.Join("",r.Select(x => x.Serial)) + string.Join("", r.Select(x => x.Number))

    Haven't checked this but seems like it should work.