Hi
I have below data
| Group 2C |
| Group 2C(i) |
| Group 3C |
| Group 3D |
| Group 3D(i) |
| Group 3E |
| Group 3G |
I want to display like below
Group 2C , Group 2C(i)
Group 3C
Group 3D , Group 3D(i)
Group 3E
Group 3G
Thanks
Hi
I have below data
| Group 2C |
| Group 2C(i) |
| Group 3C |
| Group 3D |
| Group 3D(i) |
| Group 3E |
| Group 3G |
I want to display like below
Group 2C , Group 2C(i)
Group 3C
Group 3D , Group 3D(i)
Group 3E
Group 3G
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted Jun 13, 2023, 2:45 AM EDT
To display the data as you described, you can use a loop to iterate over the groups and check if the group name ends with "(i)". If it does, you can display it on the same line as the previous group. Otherwise, you can display it on a new line. Here's an example implementation in Python:
groups = [ "Group 2C", "Group 2C(i)", "Group 3C", "Group 3D", "Group 3D(i)", "Group 3E", "Group 3G" ] # Initialize the result with the first group result = [groups[0]] # Iterate over the remaining groups starting from the second one for group in groups[1:]: # Check if the group name ends with "(i)" if group.endswith("(i)"): # Add the group to the current line result[-1] += " , " + group else: # Start a new line with the group result.append(group) # Print the result for line in result: print(line)see the output below
Group 2C , Group 2C(i) Group 3C Group 3D , Group 3D(i) Group 3E Group 3GAmit MohantyRank #112 · 20K reputationPosted Jun 13, 2023, 3:11 AM EDT
try this:
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List groups = new List { "Group 2C", "Group 2C(i)", "Group 3C", "Group 3D", "Group 3D(i)", "Group 3E", "Group 3G" }; var groupedItems = groups.GroupBy(g => g.Split('(')[0].Trim()) .Select(g => string.Join(" , ", g)); foreach (var item in groupedItems) { Console.WriteLine(item); } } }Ramco RamcoRank #462 · 5.4K reputationPosted Jun 13, 2023, 4:08 AM EDT
Hi Amit
I am getting error - Group does not contin a definition for split
BALGroup bALGroup = new BALGroup(); List objGroup = bALGroup.GetGroupList("Group"); if (objGroup != null) { var groupedItems = objGroup.GroupBy(g => g.Split('(')[0].Trim()) .Select(g => string.Join(" , ", g)); }Thanks
Vishal JoshiRank #377 · 6.7K reputationPosted Jun 13, 2023, 4:32 AM EDT
Hello Ramco,
Please try the below query with AsEnumrable to get the split function working.
var groupedItems = groups.AsEnumerable().GroupBy(g => g.Split('(')[0].Trim()) .Select(g => string.Join(" , ", g));Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Jun 13, 2023, 4:42 AM EDT
Hi Vishal
I am getting same error
List objGroup = bALGroup.GetGroupList("Group"); if (objGroup != null) { var groupedItems = objGroup.AsEnumerable().GroupBy(g => g.Split('(')[0].Trim()) .Select(g => string.Join(" , ", g)); }Thanks
Amit MohantyRank #112 · 20K reputationPosted Jun 13, 2023, 4:52 AM EDT
Assuming that the Group class has a property called "Name" that contains the group name
BALGroup bALGroup = new BALGroup(); List objGroup = bALGroup.GetGroupList("Group"); if (objGroup != null) { var groupedItems = objGroup.GroupBy(g => g.Name.Split('(')[0].Trim()) .Select(g => string.Join(" , ", g)); }Ramco RamcoRank #462 · 5.4K reputationPosted Jun 13, 2023, 5:12 AM EDT
Hi Amit
It should show the Groupname but it is displaying like image attached
Thanks
Amit MohantyRank #112 · 20K reputationPosted Jun 13, 2023, 5:18 AM EDT
Could you please list the properties that are contained within the group class? Additionally, which specific property is used to store the group name?
Ramco RamcoRank #462 · 5.4K reputationPosted Jun 13, 2023, 5:25 AM EDT
Hi AMit
I have attached image
Ramco RamcoRank #462 · 5.4K reputationPosted Jun 13, 2023, 5:30 AM EDT
Hi Amit
I have attached again
Amit MohantyRank #112 · 20K reputationPosted Jun 13, 2023, 5:39 AM EDT
Updated code
BALGroup bALGroup = new BALGroup(); List objGroup = bALGroup.GetGroupList("Group"); if (objGroup != null) { var groupedItems = objGroup.GroupBy(g => g.GroupName.Split('(')[0].Trim()) .Select(g => string.Join(" , ", g)); }Ramco RamcoRank #462 · 5.4K reputationPosted Jun 13, 2023, 5:51 AM EDT
Hi AMit
It is giving same result
Thanks
Amit MohantyRank #112 · 20K reputationPosted Jun 13, 2023, 6:11 AM EDT
try this, updated
BALGroup bALGroup = new BALGroup(); List objGroup = bALGroup.GetGroupList("Group"); if (objGroup != null) { var groupedItems = groups.GroupBy(g => g.GroupName.Split('(')[0].Trim()) .Select(g => string.Join(" , ", g.Select(item => item.GroupName))); }Vishal JoshiRank #377 · 6.7K reputationPosted Jun 13, 2023, 7:10 AM EDT
Hello
Try the below query. Instead of AsEnumerable try IQueryable.
var groupedItems = groups.AsQueryAble().GroupBy(g => g.Split('(')[0].Trim()) .Select(g => string.Join(" , ", g));Thanks