Domanda

i want to add new row to table but by passing both normal variables and array variable like the example below

int value1=1;

int value2=2;

int[] numbers = new int[] {3, 4, 5};

DataTable1.Rows.Add(value1,value2,numbers)  // numbers as single items so the row will contain 5 values (1,2,3,4,5) 

so should i build a new array and pass it ? or there a code spell to do that ? thanks

È stato utile?

Soluzione

This helper method will create a list from 1 to 5:

public IEnumerable<T> GetItemsAndCollectionsAsItems<T>(params object[] itemsAndCollections)
{
    var result = new List<T>();
    foreach (var itemOrCollection in itemsAndCollections)
    {
        var collection = itemOrCollection as IEnumerable<T>;
        if (collection == null)
        {
            result.Add((T)itemOrCollection);
        }
        else
        {
            result.AddRange(collection);
        }
    }
    return result;
}

And you call it this way:

int value1 = 1;
int value2 = 2;
int[] numbers = new int[] { 3, 4, 5 };

// Returns 1,2,3,4,5
IEnumerable<int> values = GetItemsAndCollectionsAsItems<int>(value1, value2, numbers);

Altri suggerimenti

Not sure to be happen with this Int Array but yah, have a look on this link, which stores data same as you want. Simply some tricky things have to do.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top