locked
Conat or Union not working as an extension method RRS feed

  • Question

  • User1034446946 posted

    Hi

    I am wanting to build a system which allows me to combine arrays for my include statements for use with the entity framwork, I have this extension method

    public static string[] JoinTwoIncludes(this string[] to, string[] from, string prependString)
            {
                from.Prepend(prependString);
    
                return to.Concat(from).ToArray();
                
            }

    public static string[] Prepend(this string[] to, string replaceWith)
            {
                var seperator = replaceWith.Length > 0 ? "." : "";

                for (int i = 0; i < to.Count(); i++)
                {

                    to[i] = replaceWith + seperator + to[i];
                }

                return to;
            }

    But it doesn't seem to work

    I have static methods which return string[]

    like

    public static string[] MyModel()
            {
                return new string[] { "Property1", "Property2" };
            }

    then I put them together

    var array = ModelArrays.Model1().JoinTwoIncludes(ModelArrays.Model2(), "Something").JoinTwoIncludes(ModelArrays.Model3(), "Something3");
    array.JoinTwoIncludes(ModelArrays.Model7(), "Something7")

    but array only has the base model, any advice would be appriciated.

    Thursday, July 14, 2016 10:58 AM

Answers

All replies

  • User303363814 posted

    You forgot to store the final result back into array.  That is, the last statement should be

    array = array.JoinTwoIncludes(ModelArrays.Model7(), "Something7");

    Thursday, July 14, 2016 10:54 PM
  • User-271186128 posted

    Hi enendaveyboy,

    I suggest you could refer to the following code to use Concat and Union:

            // Make an array of strings. Note that we have included spaces.
            string [] s = { "hello ", "and ", "welcome ", "to ",
                            "this ", "demo! " };
    
            // Put all the strings together.
            Console.WriteLine(string.Concat(s));
    
    // The example displays the following output:
    //       hello and welcome to this demo!

    and:

              int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
                int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };
    
                IEnumerable<int> union = ints1.Union(ints2);
    
                foreach (int num in union)
                {
                    Console.Write("{0} ", num);
                }
    
                /*
                 This code produces the following output:
    
                 5 3 9 7 8 6 4 1 0
                */

    More details, see:

    https://msdn.microsoft.com/en-us/library/0wkb0y3w(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/bb341731(v=vs.100).aspx

    https://msdn.microsoft.com/en-us/library/bb896299(v=vs.100).aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 15, 2016 9:21 AM