user3567026
user3567026

Reputation: 91

Generic method for dictionary Object with variable values and loop through it

I need to convert all my dictionary objects that coming from an api which are stored in results(model) to StudentList type.

I am doing it for one property as shown below:

StudentList<StudentData> testList = new StudentList<StudentData>();

foreach(KeyValuePair<string, IEnumerable<StudentData>> pair in result.StudentDataDetails)
{
    foreach(var x in pair.Value)
        testList.Add(x);
}

Like StudentData , there are other class objects like TeacherData, ParentData.. all these need to changed to StudentList<TeacherData>, StudentList<ParentData>.

I am trying to build a generic method for this..any help would be appreciated. I need to check if there is a dictionary property from results in the following way Dictionary<string, IEnumerable<T>> then change the dictionary as mentioned above. T is StudentData, TeacherData and so on. The generic method needs to return the StudentList<T>. Thank you

Upvotes: 0

Views: 91

Answers (3)

Manvinder Singh
Manvinder Singh

Reputation: 160

You can create the generic method which take the dictionary as input

public StudentList<T> GenericMethod(Dictionary<string, T> dictionary)
{
        StudentList<T> testList = new StudentList<T>();

        foreach(KeyValuePair<string, IEnumerable<T>> pair in dictionary)
        {
            foreach(var x in pair.Value)
                testList.Add(x);
        }

        return testList;
}

Then call this method and pass the dictionary

GenericMethod(result.StudentDataDetails);
GenericMethod(result.TeacherDataDetails);

Upvotes: 0

Gustavo Oliveira
Gustavo Oliveira

Reputation: 127

I dont know how works your classe StudentList, but, i built a sample that could help you.

public class StudentReaderAPI
{
    public StudentList<T> BuildList<T>(Dictionary<string, IEnumerable<T>> dataDetails) where T : AbstractDataDetails
    {
        if (dataDetails == null) return null;

        return new StudentList<T>(dataDetails.Values.SelectMany(x => x));
    }
}

public class StudentList<T> : List<T> where T : AbstractDataDetails
{
    public StudentList(IEnumerable<T> enumerable) : base(enumerable)
    {

    }
    //Your List logic here
}

public abstract class AbstractDataDetails
{
    //Common properties of DataDetails

    public string FirstName { get; set; }

    public string LastName { get; set; }
}

public class StudentData : AbstractDataDetails
{
    //StudentData Properties here
}

public class ParentData : AbstractDataDetails
{
    //ParentData Properties here
}

public class TeacherData : AbstractDataDetails
{
    //TeacherData Properties here
}

Upvotes: 1

Rufus L
Rufus L

Reputation: 37060

You can write a generic method that takes in a dictionary of type <string, IEnumerable<T>> and returns a List<T> from all the values. This will work for any type T, whether it's a Student, Teacher, Parent, etc:

public static List<T> GetAllValues<T>(Dictionary<string, IEnumerable<T>> input)
{
    return input?.SelectMany(kvp => kvp.Value).ToList();
}

Sample usage:

var studentDict = new Dictionary<string, IEnumerable<Student>>
{
    {"1", new List<Student> {new Student {Name = "First"}}},
    {"2", new List<Student> {new Student {Name = "Second"}}},
    {
        "3", new List<Student>
        {
            new Student {Name = "Third"},
            new Student {Name = "Fourth"}
        }
    },
};

var studentList = GetAllValues(studentDict);

studentList.ForEach(s => Console.WriteLine(s.Name));

Output

enter image description here

Upvotes: 2

Related Questions