Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an array like this

Tags:

arrays

c#

List<Customer> c= new List<Customer>()
{
    new Customer{Id = 1, Name = "Bruce"},
    new Customer{Id = 2, Name = "John"}
};

I just know

c.ForEach(o => str += o.Id.ToString() + ",");

Any ways to make it simple?

I just wanna get Id out and make Ids array int[] Ids = new {Id = 1, Id = 2}

like image 661
Bruce Woo Avatar asked Dec 19 '25 00:12

Bruce Woo


1 Answers

If you want to create an array with all those ids then you can use Select and ToArray methods :

int[] ids = c.Select(i => i.Id).ToArray();
like image 125
Zbigniew Avatar answered Dec 20 '25 12:12

Zbigniew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!