0

I'm work with IEnumerable of myObjects. Those myObjects are set by the App.config and other XML files.

I load the config in an object-model for each file and then I add the content in myObjects. So I want to merge the object-models in only one.

myObjects contains IEnumerable of myObjectChild and myObjectChild contains IEnumerable of myObjectChildChild.

I'm a little bit lost in all these IEnumerable and didn't know how to merge this.

Here are the classes:

public class MyObject
{
    public string Name { get; set; }
    public IEnumerable<MyObjectChild> MyObjectChilds { get; set; }
}

public class MyObjectChild
{
    public string Name { get; set; }
    public IEnumerable<MyObjectChildChild> MyObjectChildChilds { get; set; }
}

public class MyObjectChildChild
{
    public string Name { get; set; }
    public string Value { get; set; }
}

///////////////////////////////////////////////////////////////////////////////

EDIT ////

class Program
{
    static void Main(IEnumerable<MyObject> myObjects)
    {
        List<MyObject> objectConfigList = new List<MyObject>();
        foreach (var myO in myObjects)
        {
            if (objectConfigList.Any(a => a.Name == myO.Name))
            {
                Console.WriteLine("myO already in the list: " + myO.Name);
                Console.WriteLine("Check current myO.myOC");
                List<MyObjectChild> myOChild = myO.MyObjectChilds.ToList();
                foreach (var myOC in myOChild)
                {
                    if (objectConfigList.Where(a => a.Name == myO.Name).First().MyObjectChilds.Any(s => s.Name == myOC.Name))
                    {
                        Console.WriteLine("myOC already in the myO: " + myOC.Name);
                        List<MyObjectChildChild> tempmyOCC = myOC.MyObjectChildChilds.ToList();
                        Console.WriteLine("Check current myOC.myOCC");
                        foreach (var myOCC in tempmyOCC)
                        {
                            if (objectConfigList.Where(a => a.Name == myO.Name).First().MyObjectChilds.Where(myOCt => myOCt.Name == myOC.Name).First().MyObjectChildChilds.Any(p => p.Name == myOCC.Name))
                            {
                                Console.WriteLine("myOCC already defined in this myOCction: " + myOCC.Name);
                            }
                            else
                            {
                                objectConfigList.Where(a => a.Name == myO.Name).First().MyObjectChilds.Where(myOCc => myOCc.Name == myOC.Name).First().MyObjectChildChilds.ToList().Add(myOCC);
                            }
                        }
                    }
                    else
                    {
                        objectConfigList.Where(a => a.Name == myO.Name).First().MyObjectChilds.ToList().Add(myOC);
                    }
                }
            }
            else
            {
                objectConfigList.Add(myO);
            }
        }
    }
}

I've found a solution beacause I didn't succeed with the proposed.

I post my solution here, if you have any amelioration to propose, I'll be glad to read it !

Thanks for your help.

Greg
  • 1
  • 2
  • can you please show some piece of code? – TAHA SULTAN TEMURI Aug 23 '18 at 10:10
  • 1
    Use Distinct(). – jdweng Aug 23 '18 at 10:11
  • 1
    From the description of a problem we can only guess what's your problem. Read on [ask] and post [mcve] to get any help. – mrogal.ski Aug 23 '18 at 10:18
  • Sorry, it's my first post, I'm a little lost. I've added the classes, I work with IEnumerable and want to merge all MyObject without losing any MyObjectChildChild – Greg Aug 23 '18 at 10:32
  • You are trying to join the two sets, https://stackoverflow.com/questions/14164974/how-to-concatenate-two-ienumerablet-into-a-new-ienumerablet ? If an item can appear in both lists, but shouldn't appear in the resulting output, then you're going to have to use IEquatable then you could union them together. – tbddeveloper Aug 23 '18 at 10:47
  • Yes, I want to join the sets, I will try your solution, thanks ! – Greg Aug 23 '18 at 10:50
  • Just a side note: a property of `IEnumerable` with get and set is quite horrible. – Stefan Steinegger Aug 23 '18 at 12:49
  • Owh ! I didn't knew that, but I'm not the first to work on this project, I have to add some functionality and I'm not the author of the models. Thanks for the remark, I note that. – Greg Aug 23 '18 at 12:53

2 Answers2

0

To merge many ienumerables of the same type from different ienumerables, you have .SelectMany()

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

To merge two list you can use IEnumerable.Concat method, but this doen't ensure check duplicates

You can use IEnumerable.Distinct() to remove duplicates, but MyObject must implement IEquatable interface

public class MyObject : IEquatable<MyObject>
{
    public string Name { get; set; }
    public IEnumerable<MyObjectChild> MyObjectChilds { get; set; }
    public bool Equals<MyObject>(MyObject other) { return this.Name.Equals(other.Name);    }
}
mnieto
  • 3,744
  • 4
  • 21
  • 37