简体   繁体   中英

C# Lists, create list object from a list of objects

I have a list object like this

List<Site> lsite = GetSitesById(Id);

and the Site object is

public Site(int nId, string name, int Count)
        {
            Id = nId;
            Name = name;
            SiteOverrideCount = Count;
        }

What I need is List<int> OnlySites= which will have a list of only Site Id's

How can I get only id's from the lsite object, dump them in a list object and assign that to List OnlySites

I am new at this, hope my question made sense.

You need to use Linq Select method, eg:

List<int> OnlySites = lsite.Select(s => s.Id).ToList();

This will produce a list of site Ids.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM