Home > database >  get the distinct element(eg: ID) from student detail where collection of student details are inside
get the distinct element(eg: ID) from student detail where collection of student details are inside

Time:08-11

//Model

public class StudentDetails
    {
            
            public int Contact { get; set; }
            public string Summary { get; set; }
            public string Address { get; set; }
            public long ID { get; set; }
    
        }

//controller

public async Task AddStudDetails([FromBody] StudentDetails[] studentDetails){}

CodePudding user response:

You could use LINQ to do this:

var distinctResults = studentDetails.DistinctBy(student => student.ID).ToArray();

This'll yield an array of all the students where no two students have the same ID.

you'd also have to add a using System.Linq; to your imports :)

CodePudding user response:

var stud = from s in studentDetails select s.ID;

figured out the code I was looking for. Hope this will be helpful to others too.

  • Related