简体   繁体   中英

Parse string using C# LINQ

Need help in parse string using C# LINQ I have a string like this

11=205129022,453=8,448=CompanyID,447=D,452=63,448=userid,447=D,452=11,448=CompanyName,447=D,452=13,448=W,447=D,452=54,77=O,555=2

I would like to split this string on 448= then take an array start getting line "starts with 448" get the last string in the line which is 452= I'm trying this logic using LINQ to get the final output but it's not working.

var parties = rptstr.Split(new string[] { "453=" }, StringSplitOptions.None).Select(p => p.Split(new string[] {"448="},StringSplitOptions.None);

 var str448 = (from lnprty in parties 
                                   where lnprty.ToString().StartsWith("448=")
                                    //let tmp = line1.ToString() 
                                   select new PartyRoleModel()     
                                     {
                                         companyid == lnprty.Where(s => s.EndsWith("452=63,")).Select(s => s.Substring(s.IndexOf('=') + 1)).FirstOrDefault(),
                     userid == lnprty.Where(s => s.EndsWith("452=11,")).Select(s => s.Substring(s.IndexOf('=') + 1)).FirstOrDefault()  
                     companyname == lnprty.Where(s => s.EndsWith("452=13,")).Select(s => s.Substring(s.IndexOf('=') + 1)).FirstOrDefault() 
                     PhyFlag == lnprty.Where(s => s.EndsWith("452=54,")).Select(s => s.Substring(s.IndexOf('=') + 1)).FirstOrDefault() 
                                     })
                                .ToList();

I can split the line by 448 but 448 lines comes without 448= since it got split, I need split string also in the line so I can identify the line. Pls. remember the last line comes with other strings at end (448=W,447=D,452=54,77=O,555=2) . I should get the final output like where 452=63 get companyid(which is in 447=), 452=11 get userid, 452=13 get companyname Thanks in advance.

Try following :

        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            Dictionary<int, Dictionary<string, int>> dict = new Dictionary<int, Dictionary<string, int>>();

            StreamReader reader = new StreamReader(FILENAME);
            string input = "";
            while ((input = reader.ReadLine()) != null)
            {

                //string input = "11=205129022,453=8,448=CompanyID,447=D,452=63,448=userid,447=D,452=11,448=CompanyName,447=D,452=13,448=W,447=D,452=54,77=O,555=2";
                List<string> strArray = input.Split(new char[] { ',' }).ToList();

                //or dictionary
                Dictionary<string, int> rowDict = strArray.Select(x => x.Split(new char[] { '=' })).GroupBy(x => x.Last(), y => int.Parse(y.First())).ToDictionary(x => x.Key, y => y.FirstOrDefault());

                int id = rowDict["CompanyID"];

                dict.Add(id, rowDict);
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM