locked
how to distribute the `packet` to respective `owner` when `Name` and `Rule` matches RRS feed

  • Question

  • I have a complex case where I have to to compare 2 list of data bases on some Rules and Name and then need to distribute the packet to respective owner of the Rule. Please suggest how to approach and on how to match?

    • I have a class NameOwnersBasedOnRule where I have define my different set of Rule\Owner for a Name.

    • Number of Rule and Owner are NOT fixed for each Name.

    var nameOwnersBasedOnRule = new List<NameOwnersBasedOnRule>
             {
                 new NameOwnersBasedOnRule
                 {
                     Name = "Name1", Owners = new Dictionary<string, string>
                     {
                         {"Rule1", "Owner1"}, {"Rule2", "Owner2"}, {"Rule3", "Owner3"}
                     }
                 },
                 new NameOwnersBasedOnRule
                 {
                     Name = "Name2", Owners = new Dictionary<string, string>
                     {
                         {"Rule2", "Owner2"}
                     }
                 },
                 new NameOwnersBasedOnRule
                 {
                     Name = "Name3", Owners = new Dictionary<string, string>
                     {
                         {"Rule1", "Owner1"}, {"Rule1", "Owner2"}
                     }
                 }
             };
    • Now I have list of packet and each packet has some Rule associates with it and the Name is part of nested InstanceData. Here are some packets,

    var packets = new List<Packet>
             {
                 new Packet
                 {
                     Id = new Guid("2e08bd98-68eb-4358-8efb-9f2adedfb034"),
                     Rules = new List<string>{"Rule1", "Rule2"},
                     Results = new Result
                     {
                         ResultName = "ResultName1",
                         Instances = new List<Instance>
                         {
                             new Instance
                             {
                                 InstanceName = "InstanceName1",
                                 InstanceDatas = new List<InstanceData>
                                 {
                                     new InstanceData{Name = "Name1", Value = "V1"},
                                 }
                             }
                         }
                     }
                 },
                 new Packet
                 {
                     Id = new Guid("260ee32f-0775-49ef-b24c-632cb0b0be1a"),
                     Rules = new List<string>{"Rule1"},
                     Results = new Result
                     {
                         ResultName = "ResultName2",
                         Instances = new List<Instance>
                         {
                             new Instance
                             {
                                 InstanceName = "InstanceName2",
                                 InstanceDatas = new List<InstanceData>
                                 {
                                     new InstanceData{Name = "Name2", Value = "V2"},
                                 }
                             }
                         }
                     }
                 }
             };
    • Now I need to match Name and Rules from both list and if match I need to distribute it to the associated Owner. Same Packet will go to each owner based on match.

    • In above example data, I have 2 packets....

    • first packet with name Name1 has to duplicate to owners Owner1 and Owner2 as both rules "Rule1", "Rule2" exist here.

    • second packet does not match anything, so NO distribution.

    • I need to extract list of packets based on number of owners. For above example data, 2 list

        listOfPacketsForOwner1 =.....
      listOfPacketsForOwner2 =.....

    Here all the class structure I have,

    public class NameOwnersBasedOnRule
        {
            public string Name { get; set; }
            public Dictionary<string, string> Owners{ get; set; }
        }
    
    public class Packet
        {
            public Guid Id { get; set; }
            public List<string> Rules { get; set; }
            public Result Results { get; set; }
        }
    
        public class Result
        {
            public string ResultName { get; set; }
            public List<Instance> Instances { get; set; }
        }
    
        public class Instance
        {
            public string InstanceName { get; set; }
            public List<InstanceData> InstanceDatas { get; set; }
        }
    
        public class InstanceData
        {
            public string Name { get; set; }
            public string Value { get; set; }
        }


    Friday, September 18, 2020 3:52 AM

All replies

  • Hi Yazdani,

    Thank you for posting here.

    I wrote some code based on your example, please see if the result meets your expectations.

                 List<List<string>> result = new List<List<string>>();
               
                foreach (var packet in packets)
                {
                    List<string> listOfPacketsForOwner1 = new List<string>();
                    NameOwnersBasedOnRule n1 = nameOwnersBasedOnRule.Where(n => n.Name == packet.Results.Instances.First().InstanceDatas.First().Name).First();
                    foreach (var item in packet.Rules)
                    {
                        if (n1.Owners.ContainsKey(item))
                        {
                            listOfPacketsForOwner1.Add(n1.Owners[item]);
                        }
                    }
                    result.Add(listOfPacketsForOwner1);
                }    


    A reminder: In the third element of nameOwnersBasedOnRule, the two elements of Dictionary have the same Key "Rule1", and the Key of Dictionary needs to be unique.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 18, 2020 8:58 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 23, 2020 9:30 AM