Question concerning syntax used to check for duplicate objects

aindrea

Member
Joined
Jun 5, 2018
Messages
23
Programming Experience
1-3
I intend to check for duplicate objects contained in a list, and I have planned to use Linq for this purpose. I would like to illustrate how I achieve checking for duplicates by using student objects which have a first name, a second name as well as an age.

Here you can see the solution:

C#:
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleAppDuplicateTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> lList = new List<Student>();
            lList.Add(new Student("Joana", "Miller", 23));
            lList.Add(new Student("Peter", "Hanson", 24));
            lList.Add(new Student("Cindy", "Peterson", 26));
            lList.Add(new Student("Theodore", "Rumsfeld", 20));
            lList.Add(new Student("Adolph", "Saxe", 21));
           
            bool hasDuplicates = ((from s in lList group s by new { s.firstName, s.secondName, s.age } into g
                                                where g.Count() > 1 select g).SelectMany(g => g).Count() > 0);

            Console.WriteLine(hasDuplicates);
            Console.ReadLine();
        }
    }

  internal class Student
        {
            public string firstName;
            public string secondName;
            public int age;

            public Student(string fName, string sName, int a)
            {
                firstName = fName;
                secondName = sName;
                age = a;
            }
        }
}

What I still dislike about this solution is that I cannot access the field variables by useing the getters, as like here:

C#:
       bool hasDuplicates = ((from s in lList
                                   group s by new { s.GetFirstName(), s.GetSecondName(), s.GetAge() }
                                                into g
                                   where g.Count() > 1
                                   select g).SelectMany(g => g).Count() > 0);

The error which the compiler highlights is "invalid declaration of anonymous type member". I have been looking for a workaround of how to use the getters. Is there any workaround there?
 
C#:
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleAppDuplicateTest
{

    class Program
    {

        static void Main(string[] args)
        {
            List<Student> lList = new List<Student>();
            lList.Add(new Student("Joana", "Miller", 23));
            lList.Add(new Student("Peter", "Hanson", 24));
            lList.Add(new Student("Cindy", "Peterson", 26));
            lList.Add(new Student("Theodore", "Rumsfeld", 20));
            lList.Add(new Student("Theodore", "Rumsfeld", 20));
          
            bool hasDuplicates = ((from s in lList
                                                group s by new { firstName = s.GetFirstName(),
                                                    secondName = s.GetSecondName
                                                    () , age = s.GetAge() }
                                                into g
                                                where g.Count() > 1
                                                select g).SelectMany(g => g).Count() > 0);
 
            Console.WriteLine(hasDuplicates);
            Console.ReadLine();

        }

        private string fnMethod(Student st)
        {
            return st.GetFirstName();
        }
    }


Thank you very much for your hint - it works like this now.
 
Last edited:
Back
Top Bottom