Search code examples
linqasp.net-mvc-3model-view-controller

Select multiple columns in LINQ


I've written a LINQ query shown below :

List<Actions> actions = resourceActions.Actions.Select(s => s.ActionName).ToList();

How do I give for selecting multiple columns here ? ie I want to add columns s.ActionId and s.IsActive. I'm unable to apply it.


Solution

  • Make a class to represent the data you want:

    public class ResourceAction
    {
       public int Id {get;set;}
       public string Name {get; set; }
    }
    

    Select a list of those instead:

    List<ResourceAction> actions = resourceActions.Actions
      .Select(s => new ResourceAction() { Id = s.Id, Name = s.ActionName}).ToList();