-1

I have datatable in which one of the cell has data like "1234",0,true and I want to convert it to object array where 1234 will be string, 0 will be int and true will be of bool types respectively.

tried splitting by new object[], but failed.

Options[Name].ToString().Split(new object[] {","}, StringSplitOptions.RemoveEmptyEntries)

expected: new object[]{"1234",0,true};

chanti
  • 95
  • 1
  • 9
  • 4
    This sounds like an XY problem, why are you trying to do this? – Kieran Devlin Jul 18 '19 at 14:33
  • `"[" + "\"1234\",0,true" + "]"` and parse as JSON ... but what is a type of `Options[Name]`? (real not `object`) – Selvin Jul 18 '19 at 14:37
  • new object[]{"1234",Int.Parse("0"),Boolean.Parse("true")}; – jdweng Jul 18 '19 at 14:38
  • @Selvin Options[Name] is string saved in db. and retrived to datatable options. – chanti Jul 18 '19 at 14:51
  • @KieranDevlin Aspose.Cells.ImportTableOptions has a property public Object[] DefaultValues { get; set; } which i am trying to set using reflection. so this object array values are stored in db. and can have any data type value. – chanti Jul 18 '19 at 15:24
  • @chanti You might want to look at https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad about saving the values you have as comma separated values in the database. – Progman Jul 18 '19 at 18:08

1 Answers1

1

Maybe you are looking for this -

        var result = Options[Name].ToString()
            .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
            .Select(x => x.StartsWith("\"") ? x.Trim('\"') : (int.TryParse(x, out temp) ? (object)temp : (object)bool.Parse(x)))
            .ToArray();

Update

For a generic way to accomplish what you want is to write logicto identify type of data in a separate function GetData -

    var result = Options[Name].ToString()
        .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
        .Select(x => GetData(x))
        .ToArray();

GetData(input) function -

    private object GetData(string input)
    {
        object data;
        if (input.StartsWith("\""))
            data = input.Trim('\"');
        else if (bool.TryParse(input, out bool bTemp))
            data = bTemp;
        else if (int.TryParse(input, out int iTemp))
            data = iTemp;
        else if (double.TryParse(input, out double dTemp))
            data = dTemp;
        else
            data = input;

        return data;
    }

You can extend this method to parse input string to any possible datatype.

Hope this answers your question.

Gaurav Mathur
  • 804
  • 5
  • 14
  • Thanks this helps for the scenario I presented here. I know my approach is a little weird but I have to live with it for now so. also, it's not fixed that I will always have string, bool and int type data here. so it might need more conditions to add. so I thinking if we can have a generic type and achieve. – chanti Jul 21 '19 at 06:34
  • I have updated my answer to make the solution more generic. – Gaurav Mathur Jul 21 '19 at 11:07