Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get List<int> from List<Customer> using linq [closed]

Tags:

c#

linq

c#-3.0

I have list of customer and want its id in separate list List<int>.

I tried using:

customerList.Cast<int>.Distinct().ToList() 

But it dosn't work. I also want distinct customer int list.

How can I do that using LINQ syntax? What changes should I do in my query?

like image 357
k-s Avatar asked Nov 27 '12 11:11

k-s


2 Answers

Try this:

List<int> customerIds = customerList.Select(c => c.Id).Distinct().ToList();

The code assumes that your customer object has an Id property which returns an int.

like image 64
roomaroo Avatar answered Oct 31 '22 18:10

roomaroo


Select the ID from your Customer List and then use ToList to get a list of Ids.

var IdList = customerList.Select(r=> r.ID).ToList();

To get distinct Ids try:

var IdList = customerList.Select(r=> r.ID).Distinct().ToList();
like image 45
Habib Avatar answered Oct 31 '22 19:10

Habib