none
Structural analysis and create with the help of LinQ and a good interface RRS feed

  • Question

  • Hello,
     ---> to customer, 9 arguments:
    [1]: ID|20200011
    [2]: REF|9919000000011001
    [3]: SNO|9919000000011001|001|0
    [4]: SNO|9919000000011002|002|0
    [5]: SNO|9919000000011003|003|0
    [6]: SNO|9919000000011004|004|0
    [7]: SNO_SUM|4
    [8]: GROUP|A
    [9]: CATEGORY|A|PriceSegment-C
    model
    
    public int custExchangeFunct(string[] inArgs, out string[] outArgs, out string errorInterface);
    I need to send requests to an interface that can look like this.
    The structure is always key value. The key can have several values separated by the character |
    Example
    ID = 20200011
    SNO = 9919000000011003 Subunit 003 next is 0

    I'm looking for ways to create this the easiest way. With interface, class.
    ListMaster.Add(key, value)

    ListMaster.Add(key, ListDetail.join(|) )
    If I get such a structure back from the customer, how can I quickly analyze it into objects?

    string res = string.Join("|", Liste);
    ///////////////////////////////
    
    public class MasterDetail
    {
    	public string Key { set; get; }
    	public List<string> Value;
    
    	public MasterDetail()
    	{
    		Value = new List<string>();
    	}
    }
    string inp = @"SNO|9919000000011003|003|0|4214|314424";
    MasterDetail testMasterDetail = new MasterDetail();
    int index = inp.IndexOf('|');
    testMasterDetail.Key = inp.Substring(0, index);
    string detail = inp.Substring(index + 1, inp.Length - (index+1));
    testMasterDetail.Value.AddRange(from item in detail.Split('|') select item);
    
    //  Master 
    //        List with key, value
    //                       value == List with values
    So I can work more easily.
    Thanks in advance for tipps.
    Best regards Markus



    Monday, October 7, 2019 5:00 PM

Answers

  • You can save a couple of steps by using Split from the start:

    {
            string inp = "SNO|99190000000011003|003|0|4214|314424";
            List<string> parts = new List<string>(inp.Split('|'));
            string key = parts[0];
            parts.RemoveAt(0);
            Console.WriteLine( key );
            foreach( var p in parts )
            {
                Console.WriteLine( p );
            }
        }


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, October 7, 2019 6:34 PM