Click here to Skip to main content
15,912,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I want to convert object type to nullable int array (int?[]) in c# but I am not getting nay solution, can anyone know how to do this.
C#
int?[] NullableIntArray{get;set;}

object value = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

NullableIntArray = value;


((IEnumerable)propertyValue).Cast<object>().Select(x => x.ToString()).ToArray(); using this I can convert object to string array
where
propertyValue = ["","",""];


likie this I want object to int?[].


Thnaks
sushil

What I have tried:

((IEnumerable)propertyValue).Cast
Posted
Updated 28-Sep-16 2:10am
v4

That will work. Does it now?

Personally, I wouldn't do it quite like that (but close):

C#
int?[] result = null;

var enumerable = propertyValue as IEnumerable; 
// will be null if not IEnumerable.  These checks are always valid

if(enumerable != null && enumerable.All(i=>i is int?) //or is INullable<int>.  they should be the same
  result = enumerable.Cast<int?>().ToArray()

return result;



This will return either a int?[] or null.

Personally I would add checks that throw exceptions so I can identify why I'm getting null. After all, it might be a different object, or a null int?[].

This did not work as "null is int?" always equals false.

Instead, replace with "i=>i==null || i is int"

Here is the full unit test that passes:

C#
[TestMethod]
public void TryPart1()
{

    int?[] value = new int?[10];
    for (int i = 0; i < value.Length; i++)
    {
        value[i] = (i > 4) ? (int?)null : 0;
    }

    object a = value;
    var result = TryPart2(a);

    Assert.IsTrue(result != null);
    Assert.IsTrue(result.Length == 10);

}

public int?[] TryPart2(object a)
{

    int?[] result = null;

    var enumerable = a as IEnumerable;

    if (enumerable != null)
    {
        var objects = enumerable.Cast<object>().ToArray();
        if (objects.All(o => o==null || o is int))
            result = objects.Cast<int?>().ToArray();

    }

    return result;
}
 
Share this answer
 
v5
Comments
Member 11859517 26-Sep-16 8:45am    
Thanks Andy Lang,
it is not working for me giving error

An unhandled exception of type 'System.InvalidCastException' occurred in System.Core.dll
Additional information: Specified cast is not valid.
Andy Lanng 26-Sep-16 8:47am    
k - hold on - gonna test
Member 11859517 26-Sep-16 8:48am    
ok.
Member 11859517 26-Sep-16 8:47am    
I am doing like something
public int?[] DEVICE_MAX_FILE_SIZE { get; set; }

var enumerable = propertyValue as IEnumerable;

DEVICE_MAX_FILE_SIZE = enumerable.Cast
Andy Lanng 26-Sep-16 9:04am    
Just use the TryPart2 method to do it for you.
You need to to do it something like this, i have tested this code on my machine:

C#
using System;
using System.Linq;
using System.Collections.Generic;
public class HelloWorld
{
	public static void Main()
	{
		
        int?[] NullableIntArray;
        object value = new object[]{"a",0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int result;
        NullableIntArray = ((IEnumerable<object>)value)
		                   .Select(x=>int.TryParse(x.ToString(),out result) ? (int?)result : (int?)null)
                           .ToArray();
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900