2

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.

1 Answer 1

3

You need to use Linq Select method, e.g.:

var onlySites = lsite.Select(s => s.Id).ToList();

This will produce a list of site Ids.

Sign up to request clarification or add additional context in comments.

4 Comments

That worked like a charm, I am not very familiar with => operator, is there a good tutorial on this. It looks like a very handy feature.
@Sarah, this is a lambda expression. Have a look: learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…
Thanks, what about exstending it to return a sorted string[]? At the moment I have: var listGroupNames = _PublisherData.FieldServiceGroups.Select(s => s.Name).ToList(); aryStrGroupNames = listGroupNames.OrderBy(p => p).ToArray();. Can it be don't all on one line?
@AndrewTruckle, var aryStrGroupNames = _PublisherData.FieldServiceGroups.Select(s => s.Name).OrderBy(p => p).ToArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.