cavillac
cavillac

Reputation: 1311

Getting one list from another list using LINQ

I've seen the technique of taken a list of ObjectA and converting into a list of ObjectB where the two classes share some similar properties but is there an easier way to do that when you're just going from a list of ObjectA to another list of ObjectA?

Basically I want to do ...

 var excv = from AuditedUser in data
    where AuditedUser.IsMarkedForRemoval == false
    select AuditedUser;

... but instead of a var I want the results to form a new List < AuditedUser > .

Is there something super easy I'm just missing?

Upvotes: 2

Views: 1045

Answers (1)

Paul Mendoza
Paul Mendoza

Reputation: 5787

 var excv = (from AuditedUser in data
    where AuditedUser.IsMarkedForRemoval == false
    select AuditedUser).ToList();

I wrapped your LINQ statement with parens and added the ToList call at the end. Is this what you're looking for?

You could also do the following which is shorter to type and read I think:

List<AuditUser> excv = data.Where(a=>!a.IsMarkedForRemoval).ToList();

Upvotes: 7

Related Questions