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?
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With