1

I have a object of class 'PersonnelVehicle' which has structure as below:

public class PersonnelVehicle
{
    private Guid _personnelId;
    public Guid PersonnelId
    {
        get
        {
            return _personnelId;
        }
    }

    private int _Age;
    public int Age
    {
        get
        {
            return _Age;
        }
    }
    private string _personnelName;
    public string PersonnelName
    {
        get
        {
            return _personnelName;
        }
    }
}

I know how to fetch single element using lambda expression

i.e ObjectPersonnel.Select(x=>x.PersonnelId)

1) But how to fetch multiple elements

i.e Age and PersonnelName from this object using Lambda expression?

2) Also, if I have structure for PersonnelVehicle as below,

public class PersonnelVehicle
{
public ObservableCollection<PersonnelModel> Personnel_List = new    ObservableCollection<PersonnelModel>
        {
             new PersonnelModel{ID=Guid.NewGuid(),Name="Mr.Joe",Gender="Male",Hospital="Poly Clinic",EMPID="abc 123",Capabilities="123",Position="Assistant",Title="Test",Status="General",ICNumber="IC 123",Roles="Test"},
             new PersonnelModel{ID=Guid.NewGuid(),Name="Su Su",Gender="Female",Hospital="Clementi Clinic",EMPID="abc 1234",Capabilities="1234",Position="Security",Title="Test",Status="General",ICNumber="IC 1234",Roles="Test"},
              new PersonnelModel{ID=Guid.NewGuid(),Name="Ms Tan",Gender="Female",Hospital="Bishan Clinic",EMPID="abc 1235",Capabilities="1235",Position="HR",Title="Test",Status="General",ICNumber="IC 1235",Roles="Test"},
        };
}

How to fetch all the list of IDs and Positions using same way?

Note: I need only in Lambda expressions, so its different question from this link: Linq Syntax - Selecting multiple columns

Community
  • 1
  • 1
Pratik
  • 161
  • 4
  • 13

2 Answers2

3

Use an anonymous type

ObjectPersonnel.Select(x=> new { x.PersonnelId , x.Age }),
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • If I have object Personnel_List inside 'ObjectPersonnel' as below, Personnel_List = new ObservableCollection { new PersonnelModel{ID=Guid.NewGuid(),Name="Mr.Joe",Gender="Male",EMPID="abc 123",Capabilities="123",Position="Assistant",Title="Test",Status="General",ICNumber="IC 123",Roles="Test"},new PersonnelModel{ID=Guid.NewGuid(),Name="Su Su",Gender="Female",EMPID="abc 1234",Capabilities="1234",Position="Security",Title="Test",Status="General",ICNumber="IC 1234",Roles="Test"},}; how to fetch just ID and Gender? – Pratik Aug 08 '14 at 01:15
0

Do you need to name the variables in there?

ObjectPersonnel.Select(x=> new { PersonnelId = x.PersonnelId , Age = x.Age })
Derek
  • 7,615
  • 5
  • 33
  • 58