3

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.

2
  • Take a look at stackoverflow.com/a/11592103/985284 Commented Aug 10, 2012 at 10:02
  • cant you use a container or something. so resourceActions.Actions.Select(s => new Container(s.ActionName,...)).ToList();.. you will obviously need to find a suitable container Commented Aug 10, 2012 at 10:03

3 Answers 3

12

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();
Sign up to request clarification or add additional context in comments.

Comments

2

I believe this is what your looking for. However you need to change the output to an anonymous type.

var actions = resourceActions.Actions.Select(s => new { s.ActionName, s.ActionId, s.IsActive } ).ToList();

Comments

2

You can use a anonymous type for this, for example

var actions = resourceActions.Actions.Select(s => 
    new { Id = s.Id, Name = s.ActionName, Active = s.IsActive).ToList();

but a better way would be to create a class like

public class ActionWithId
{
   public int Id { get; set; }
   public string Name { get; set; }
   public bool Active { get; set; }
}

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

Comments

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.