How to extract a collection of different values ​​from a list of <T> user objects?

I have a list of objects - let them say that these are orders.

Order
OrderID
Date
SalesmanId
...

I want to extract a Distinctlist SalesmanIdfrom this list. What is the best way to do this? I don’t assume that he is looping by hand ... is this?

UPDATE Thank you for your answers. I thought of an additional requirement (outlined after John Skets' answer) and encoded it as follows:

var salesusers = from s in lstOrders 
                 group s by new { s.SalesUserId,s.Username} 
                 into g  
                 select new { UserName = g.Key.Username, UserId = g.Key.SalesUserId };

This works, but I'm not sure if this is the right approach or if I am behind the mark?

Thank.

UPDATE # 2: This one ran and ran - beginners like me might find answers to this related question also useful.

+3
source share
2

SalesmanIds, :

var salesmanIds = orders.Select(x => x.SalesmanId)
                        .Distinct();

ToList(), List<T>.

using System.Linq.

EDIT: , , :

var salesmanIds = orders.Select(x => new { x.SalesmanId, x.UserName })
                        .Distinct();
+11

:

IEqualityComparer<T> ( .

:

IEnumerable<Order> myDistinctOrders = oredersList.Distinct();
+3

All Articles