Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Need a solution for below problem in C#/LINQ where I have to convert below inout string to output string.

Input String -
App.MName, App.LName, CoApp.LName, CoApp.CurrRes.Address, Veh.Year , LS.TotalAmount, TrdIn.Year

Expected Output String -
App(MName,LName..),CoApp(LName, CurrRes(Address,..),..),Veh(Year,..),LS(TotaAmount,..), TrdIn(Year)

Basically Grouping the Parent Together to avoid repeated names.

What I have tried:

Have tried various string functions , however finding it difficult to nest one parent under another.
Posted
Updated 4-Nov-17 20:43pm
Comments
PIEBALDconsult 4-Nov-17 22:39pm    
Chunk it into an XmlDocument then re-chunk it back out?
Karthik_Mahalingam 5-Nov-17 0:42am    
what is the max number of parent-child hierarchy ?
example: CoApp.CurrRes.Address- 3
Member 13503622 5-Nov-17 0:43am    
5
Karthik_Mahalingam 5-Nov-17 0:44am    
ok.
PIEBALDconsult 5-Nov-17 1:02am    
I'm going for no limit.

A little late but here is a compact LINQ solution:
C#
class Program
{
    static void Main(string[] args)
    {
        var input = "App.MName, App.LName, CoApp.xxx.yyy.zzz, CoApp.LName, CoApp.CurrRes.Address, CoApp.CurrRes.Address.SubAddress1, CoApp.CurrRes.Address.SubAddress2, Veh.Year , LS.TotalAmount, TrdIn.Year";

        var result = string.Join(",", input.Split(new[] { ',', ' ' },
                                 StringSplitOptions.RemoveEmptyEntries)
                           .Select(x => x.Split(new[] { '.' }).ToList())
                           .GroupBy(y => y[0])
                           .Select(x => CustomGroup(x)));

        Console.WriteLine(input);
        Console.WriteLine();
        Console.WriteLine(result);
        Console.ReadKey();
    }

    private static string CustomGroup(IGrouping<string, IEnumerable<string>> group)
    {
        var items = group.Where(x => x.Skip(1).Any());
        return items.Any() 
            ? new StringBuilder()
                .Append(group.Key)
                .Append("(")
                .Append(string.Join(",", items.Select(x => x.Skip(1))
                                                .GroupBy(y => y.First())
                                                .Select(x => CustomGroup(x))))
                .Append(")")
                .ToString() 
            : group.Key;
    }
}

Outputs:
App.MName, App.LName, CoApp.xxx.yyy.zzz, CoApp.LName, CoApp.CurrRes.Address, CoApp.CurrRes.Address.SubAddress1, CoApp.CurrRes.Address.SubAddress2, Veh.Year , LS.TotalAmount, TrdIn.Year

App(MName,LName),CoApp(xxx(yyy(zzz)),LName,CurrRes(Address(SubAddress1,SubAddress2))),Veh(Year),LS(TotalAmount),TrdIn(Year)
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 5-Nov-17 2:54am    
5!
C#
namespace Foo
{
  public static partial class Bar
  {
    private static readonly System.Text.RegularExpressions.Regex reg ;

    static Bar
    (
    )
    {
      reg = new System.Text.RegularExpressions.Regex
      (
        @"(\w+)(?:\.(\w+)*)*"
      ,
        System.Text.RegularExpressions.RegexOptions.Compiled
      ) ;

      return ;
    }

    public static string
    Baz
    (
      string Input
    )
    {
      System.Text.StringBuilder result = new System.Text.StringBuilder ( Input.Length ) ;

      System.Text.RegularExpressions.MatchCollection mc = reg.Matches ( Input ) ;

      System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;

      System.Xml.XmlNode ele = doc.CreateElement ( "Hierarchy" ) ;

      doc.AppendChild ( ele ) ;

      for ( int i = 0 ; i < mc.Count ; i++ )
      {
        ele = doc.DocumentElement ;

        for ( int j = 1 ; j < mc [ i ].Groups.Count ; j++ )
        {
          for ( int k = 0 ; k < mc [ i ].Groups [ j ].Captures.Count ; k++ )
          {
            string val = mc [ i ].Groups [ j ].Captures [ k ].Value ;

            System.Xml.XmlNode tmp = ele.SelectSingleNode ( val ) ;

            if ( tmp == null )
            {
              tmp = doc.CreateElement ( val ) ;

              ele.AppendChild ( tmp ) ;
            }

            ele = tmp ;
          }
        }
      }

      Foz ( doc.DocumentElement.ChildNodes , result ) ;

      return ( result.ToString() ) ;
    }

    private static void
    Foz
    (
      System.Xml.XmlNodeList    Node
    ,
      System.Text.StringBuilder Output
    )
    {
      for ( int i = 0 ; i < Node.Count ; i++ )
      {
        Output.Append ( Node [ i ].Name ) ;

        if ( Node [ i ].HasChildNodes )
        {
          Output.Append ( "(" ) ;

          Foz ( Node [ i ].ChildNodes , Output ) ;

          Output.Append ( ")" ) ;
        }

        Output.Append ( "," ) ;
      }

      Output.Length-- ;

      return ;
    }
  }
}


System.Console.WriteLine ( Foo.Bar.Baz ( @"App.MName, App.LName, CoApp.LName, CoApp.CurrRes.Address, Veh.Year , LS.TotalAmount, TrdIn.Year" ) ) ;

App(MName,LName),CoApp(LName,CurrRes(Address)),Veh(Year),LS(TotalAmount),TrdIn(Year)
 
Share this answer
 
Comments
Karthik_Mahalingam 5-Nov-17 1:36am    
High Five !!!

brilliant
PIEBALDconsult 5-Nov-17 1:09am    
Thanks.
Graeme_Grant 5-Nov-17 2:12am    
Close but not linq... Linq version below.. ;)
PIEBALDconsult 5-Nov-17 8:42am    
Linq is filth.
Graeme_Grant 5-Nov-17 9:43am    
LMAO... Beauty is in the eye of the beholder but in your case you have a case of stink-eye[^]... ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900