locked
Converting int[] to int?[] RRS feed

  • Question

  • Hi

    I am trying to fix an application where a method signature in a dependent dll changed its type from int[] to int?[].  Is there a clean way to change this? 

    for example.  I have an Int32[] parameter passed in called ordernumbers

    It would appear that if I did this:

    System.Collections.Generic.List<int?> list = new System.Collections.Generic.List<int?>();

    for(int=0;i<ordernumbers.length; i++)

    {

    list.Add(ordernumbers[i]);

    }

    int?[] convertedList = list.ToArray();

    this should work.  I would then pass this to the underlying method that now requires an int?[] whereas before it was an int[].  I would think that there should be an easier/more terse way to do this.  I have a number of these to convert.

    Thanks

    
    
    
    
    Thursday, October 3, 2013 2:56 PM

Answers

  • Here is another simple Linq solution.

    int[] x = { 1, 2, 3 };
    int?[] y = x.Cast<int?>().ToArray();

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, October 3, 2013 4:37 PM
  • Hi Cheese,

    what about using a LINQ-statement. :-)

    int[] yourIntArray = new int[] { 1, 2, 3 };
    int?[] converted = yourIntArray.Select(i => (int?)i).ToArray();


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    • Proposed as answer by Jason Dot Wang Wednesday, October 9, 2013 6:08 AM
    • Marked as answer by Jason Dot Wang Monday, October 14, 2013 2:38 AM
    Thursday, October 3, 2013 3:35 PM
  • Hi Cheese,

    my solution from above you can also put into an extension method:

     public static class ArrayExtensions
        {
            public static T?[] ToNullableArray<T>(this T[] array) where T : struct
            {
                return array.Select(i => (T?)i).ToArray();
            }
        }

    And then use it on any value type array like this:

      int[] yourIntarry = new int[] { 1, 2, 3 };
      int?[] converted = yourIntarry.ToNullableArray();

    :-)


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


    Thursday, October 3, 2013 4:10 PM
  • Here are a couple useful methods I have and you can modify

    //Convert any list object to a datatable
            public DataTable ConvertToDataTable<T>(IList<T> data)
            {
                PropertyDescriptorCollection properties =
                   TypeDescriptor.GetProperties(typeof(T));
                DataTable table = new DataTable();
                foreach (PropertyDescriptor prop in properties)
                    table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                foreach (T item in data)
                {
                    DataRow row = table.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                        row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                    table.Rows.Add(row);
                }
                return table;
            }
            public static DataSet ConvertToDataSet<T>(IList<T> list)
            {
                Type elementType = typeof(T);
                DataSet ds = new DataSet();
                DataTable t = new DataTable();
                ds.Tables.Add(t);
                //add a column to table for each public property on T
                foreach (var propInfo in elementType.GetProperties())
                {
                    //t.Columns.Add(propInfo.Name, propInfo.PropertyType);
                    Type ColType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;
                    t.Columns.Add(propInfo.Name, ColType);
                }
                //go through each property on T and add each value to the table
                foreach (T item in list)
                {
                    DataRow row = t.NewRow();
                    foreach (var propInfo in elementType.GetProperties())
                    {
                        //row[propInfo.Name] = propInfo.GetValue(item, null);
                        row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value;
                    }
                    t.Rows.Add(row);
                }
                return ds;
            }

    Edit: I ran this code and note what you need to do

    int?[] testnullar = { null, 1, 3, 4 };
    var testnullar2 = testnullar.ToArray();
    int?[] testnullar3 = null;
    //If you don't check for null conversion will have an exception
    if (testnullar3 != null)
    {
        var testnular4 = testnullar3.ToArray();
    }


    • Edited by PaulDAndrea Thursday, October 3, 2013 3:42 PM
    • Proposed as answer by PaulDAndrea Monday, October 7, 2013 4:37 PM
    • Marked as answer by Jason Dot Wang Monday, October 14, 2013 2:38 AM
    Thursday, October 3, 2013 3:30 PM

All replies

  • Here are a couple useful methods I have and you can modify

    //Convert any list object to a datatable
            public DataTable ConvertToDataTable<T>(IList<T> data)
            {
                PropertyDescriptorCollection properties =
                   TypeDescriptor.GetProperties(typeof(T));
                DataTable table = new DataTable();
                foreach (PropertyDescriptor prop in properties)
                    table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                foreach (T item in data)
                {
                    DataRow row = table.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                        row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                    table.Rows.Add(row);
                }
                return table;
            }
            public static DataSet ConvertToDataSet<T>(IList<T> list)
            {
                Type elementType = typeof(T);
                DataSet ds = new DataSet();
                DataTable t = new DataTable();
                ds.Tables.Add(t);
                //add a column to table for each public property on T
                foreach (var propInfo in elementType.GetProperties())
                {
                    //t.Columns.Add(propInfo.Name, propInfo.PropertyType);
                    Type ColType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;
                    t.Columns.Add(propInfo.Name, ColType);
                }
                //go through each property on T and add each value to the table
                foreach (T item in list)
                {
                    DataRow row = t.NewRow();
                    foreach (var propInfo in elementType.GetProperties())
                    {
                        //row[propInfo.Name] = propInfo.GetValue(item, null);
                        row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value;
                    }
                    t.Rows.Add(row);
                }
                return ds;
            }

    Edit: I ran this code and note what you need to do

    int?[] testnullar = { null, 1, 3, 4 };
    var testnullar2 = testnullar.ToArray();
    int?[] testnullar3 = null;
    //If you don't check for null conversion will have an exception
    if (testnullar3 != null)
    {
        var testnular4 = testnullar3.ToArray();
    }


    • Edited by PaulDAndrea Thursday, October 3, 2013 3:42 PM
    • Proposed as answer by PaulDAndrea Monday, October 7, 2013 4:37 PM
    • Marked as answer by Jason Dot Wang Monday, October 14, 2013 2:38 AM
    Thursday, October 3, 2013 3:30 PM
  • Hi Cheese,

    what about using a LINQ-statement. :-)

    int[] yourIntArray = new int[] { 1, 2, 3 };
    int?[] converted = yourIntArray.Select(i => (int?)i).ToArray();


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    • Proposed as answer by Jason Dot Wang Wednesday, October 9, 2013 6:08 AM
    • Marked as answer by Jason Dot Wang Monday, October 14, 2013 2:38 AM
    Thursday, October 3, 2013 3:35 PM
  • Hi Cheese,

    my solution from above you can also put into an extension method:

     public static class ArrayExtensions
        {
            public static T?[] ToNullableArray<T>(this T[] array) where T : struct
            {
                return array.Select(i => (T?)i).ToArray();
            }
        }

    And then use it on any value type array like this:

      int[] yourIntarry = new int[] { 1, 2, 3 };
      int?[] converted = yourIntarry.ToNullableArray();

    :-)


    Thomas Claudius Huber

    "If you can´t make your app run faster, make it at least look & feel extremly fast"

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


    Thursday, October 3, 2013 4:10 PM
  • Maybe one of the dbTypes may work like VarNumeric

    namespace System.Data
    {
        // Summary:
        //     Specifies the data type of a field, a property, or a Parameter object of
        //     a .NET Framework data provider.
        public enum DbType
        {
            // Summary:
            //     A variable-length stream of non-Unicode characters ranging between 1 and
            //     8,000 characters.
            AnsiString = 0,
            //
            // Summary:
            //     A variable-length stream of binary data ranging between 1 and 8,000 bytes.
            Binary = 1,
            //
            // Summary:
            //     An 8-bit unsigned integer ranging in value from 0 to 255.
            Byte = 2,
            //
            // Summary:
            //     A simple type representing Boolean values of true or false.
            Boolean = 3,
            //
            // Summary:
            //     A currency value ranging from -2 63 (or -922,337,203,685,477.5808) to 2 63
            //     -1 (or +922,337,203,685,477.5807) with an accuracy to a ten-thousandth of
            //     a currency unit.
            Currency = 4,
            //
            // Summary:
            //     A type representing a date value.
            Date = 5,
            //
            // Summary:
            //     A type representing a date and time value.
            DateTime = 6,
            //
            // Summary:
            //     A simple type representing values ranging from 1.0 x 10 -28 to approximately
            //     7.9 x 10 28 with 28-29 significant digits.
            Decimal = 7,
            //
            // Summary:
            //     A floating point type representing values ranging from approximately 5.0
            //     x 10 -324 to 1.7 x 10 308 with a precision of 15-16 digits.
            Double = 8,
            //
            // Summary:
            //     A globally unique identifier (or GUID).
            Guid = 9,
            //
            // Summary:
            //     An integral type representing signed 16-bit integers with values between
            //     -32768 and 32767.
            Int16 = 10,
            //
            // Summary:
            //     An integral type representing signed 32-bit integers with values between
            //     -2147483648 and 2147483647.
            Int32 = 11,
            //
            // Summary:
            //     An integral type representing signed 64-bit integers with values between
            //     -9223372036854775808 and 9223372036854775807.
            Int64 = 12,
            //
            // Summary:
            //     A general type representing any reference or value type not explicitly represented
            //     by another DbType value.
            Object = 13,
            //
            // Summary:
            //     An integral type representing signed 8-bit integers with values between -128
            //     and 127.
            SByte = 14,
            //
            // Summary:
            //     A floating point type representing values ranging from approximately 1.5
            //     x 10 -45 to 3.4 x 10 38 with a precision of 7 digits.
            Single = 15,
            //
            // Summary:
            //     A type representing Unicode character strings.
            String = 16,
            //
            // Summary:
            //     A type representing a time value.
            Time = 17,
            //
            // Summary:
            //     An integral type representing unsigned 16-bit integers with values between
            //     0 and 65535.
            UInt16 = 18,
            //
            // Summary:
            //     An integral type representing unsigned 32-bit integers with values between
            //     0 and 4294967295.
            UInt32 = 19,
            //
            // Summary:
            //     An integral type representing unsigned 64-bit integers with values between
            //     0 and 18446744073709551615.
            UInt64 = 20,
            //
            // Summary:
            //     A variable-length numeric value.
            VarNumeric = 21,
            //
            // Summary:
            //     A fixed-length stream of non-Unicode characters.
            AnsiStringFixedLength = 22,
            //
            // Summary:
            //     A fixed-length string of Unicode characters.
            StringFixedLength = 23,
            //
            // Summary:
            //     A parsed representation of an XML document or fragment.
            Xml = 25,
            //
            // Summary:
            //     Date and time data. Date value range is from January 1,1 AD through December
            //     31, 9999 AD. Time value range is 00:00:00 through 23:59:59.9999999 with an
            //     accuracy of 100 nanoseconds.
            DateTime2 = 26,
            //
            // Summary:
            //     Date and time data with time zone awareness. Date value range is from January
            //     1,1 AD through December 31, 9999 AD. Time value range is 00:00:00 through
            //     23:59:59.9999999 with an accuracy of 100 nanoseconds. Time zone value range
            //     is -14:00 through +14:00.
            DateTimeOffset = 27,
        }
    }


    jdweng

    Thursday, October 3, 2013 4:11 PM
  • Here is another simple Linq solution.

    int[] x = { 1, 2, 3 };
    int?[] y = x.Cast<int?>().ToArray();

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, October 3, 2013 4:37 PM