Query on multiple fields with a concatenate string field


Library ID_1 ID_2 City Book

Pippo 1 010 NA AA
Pippo 1 010 NA AA
Pippo 1 010 TO CC
Pluto 2 011 RM AA
Pluto 2 011 TO BB
Pluto 2 011 RM BB
Pluto 2 011 RM CC

I need as output the following table:
Library ID_1 ID_2 No. Books Detail
Pippo 1 010 3 #2 NA, #1 TO
Pluto 2 011 4 #3 RM, #1 TO

I have tried to generate a key composed of key=“Library + City”. Add this field to the datable, then use the first query to compute the count of books for the new “key”, and
again a second query to compute the total for Library.

I know that there is a simple way to do the process with a single query using “concatenate” but I am not able to write it correctly.

May you help me?

Thanks very much

Hi @Paola58 ,
I am attaching a sample code for your reference which might help you.
Table.xaml (11.5 KB)

Your output table will be in “Output_String.xlsx” file after you run the above code.

If Table headers changes change it in build data table

Thanks & Regards,
Shubham Dutta

Let us summarize our understanding:

group the data by Library (maybe other additional keys) and retrieve:
group count = No,
Books count for the different Cities of a group

So we can prototype it with a LINQ to following (some col names simplified)



(From d In dtData.AsEnumerable
Group d By k=d("LIB").toString.Trim Into grp=Group
Let cnt = grp.Count
Let dctDet = grp.GroupBy(Function (g1) g1("City").toString.Trim).ToDictionary(Function (lk) lk.Key, Function (lk) lk.count)
Let strDet = String.Join(",", dctDet.Select(Function (kvp) String.Format("#{0} {1}", kvp.Value.ToString, kvp.Key)))
Let ra = grp.First.ItemArray.Take(3).Concat({cnt, strDet}).ToArray
Select r = dtResult.Rows.Add(ra)).CopyToDataTable

Dont fear the LINQ as we can take it also and decompose it into a NON-LINQ implementation.

Also have a look here:

It works!

Thanks a lot!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.