4

Class structure

 public class EmployeeDetails
 {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Exp { get; set; }
 }

List of employee details

Id Name       Exp
-- ---------  --------
1  Bill       2 years
2  John       5 years
3  Doug       1 years

I want to remove one field form list object like Id, like below output

Name       Exp
---------  --------
Bill       2 years
John       5 years
Doug       1 years

anyone have an idea how to do?
share with me
Thank you

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Pradip Talaviya
  • 399
  • 2
  • 8
  • 22

4 Answers4

13

You can try:

var listWithoutCol = List.Select(x => new { x.Name , x.Exp}).ToList();

that will return a List with only the information of the fields Name and Exp...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

If you want the new list to have a concrete type objects instead of anonymous objects, you can define yourself a corresponding class without IDs:

public class EmployeeDetailsDTO
{
    public string Name { get; set; }
    public string Exp { get; set; }
}

and use Select as:

var listWithoutIDs = List.Select(x => new EmployeeDetailsDTO{ Name = x.Name , Exp = x.Exp}).ToList();
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
0
var employeeDetails = new List<EmployeDetails> { Id=1, Name="Peter", Exp="Two years" };   
var listWithoutId = employeeDetails.Select(x => new { x.Name, x.Exp}).ToList();
XardasLord
  • 1,764
  • 2
  • 20
  • 45
0

You can use JsonIgnore

 public class EmployeeDetails
 {
        [JsonIgnore]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Exp { get; set; }
 }
  • It's worth to mention that `JsonIgnore` was referencing `Newtonsoft.Json` and applicable for serialization scenario only. – Zephyr Jul 21 '20 at 07:51