-1

I have a 4 classes:
Person - Abstract Class
Student - Class which inherit from Person
Teacher - Class which inherit from Person
PersonProvider - Static class which creates a list<A> and this list keep objects of Student and Teacher

Now, problem is, I have below query which show the user that his choice is either he has to choose between student or teacher but how to pass what he has chosen to next query (so list all student or all teachers)? I can't just read from the console and pass it to my query as typeOf() and getType() does not work with string :).

Below query return distinct types from the list but I do not know how to proceed further:

var listOfPersons = PersonProvider
  .AvailablePersons
  .Where(p => p.GetType().BaseType == typeof(Person))
  .GroupBy(p => p.GetType().Name)
  .Select(g => g.First())
  .ToList();

foreach (var item in listOfPersons )
{
    Console.WriteLine(item.GetType().Name);
}

This returns:

Student
Techaer

And now user should type in the console "Student" or Teacher so I could pass it to the next query which I dont know how to build. I was thinking about generic extension method with **Func<T, bool> predicate** but I'm not sure about it.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
h00jraq
  • 21
  • 2
  • 1
    Is correct to say that you mean, `A` is the `Person` class, `B` is the `Student` and `C` is `Teacher`? – Felipe Oriani May 03 '21 at 19:21
  • Not a well asked question, is it? Perhaps what you are asking is how to create an instance of a type from a string name? If so, look here: https://stackoverflow.com/questions/7598088/purpose-of-activator-createinstance-with-example – g01d May 03 '21 at 19:25
  • I dont understand the question. If the user can select between the `Student` and `Teacher` you know exactly what type it refers to. So no need to interpret strings, a `switch` is enough. – Tim Schmelter May 03 '21 at 19:28
  • Sorry, I was edditing it and I did some typos. I promise I'll do better next type. I've figured out (with friend help) that I can use GetType().Name ... – h00jraq May 03 '21 at 19:29
  • 3
    Side note: `.Where(p => p.GetType().BaseType == typeof(Person))` can be written as `.OfType()` – Dmitry Bychenko May 03 '21 at 19:34

1 Answers1

1

You could try to convert the input into an Type and search for instance in the static list which ones are from the same type.

For sample:

string input = "Teacher";
Type inputType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(t => t.Name == input);

// validate if the user input is correct.
if (inputType == null)
    throw new ArgumentOutOfRangeException(nameof(input), "It must be Student or Teacher.");
    

var listOfPersons = PersonProvider.AvailablePersons.Where(p => p.GetType() == inputType)                                
                                  .ToList();

// print the names
foreach (var item in listOfPersons)
    Console.WriteLine(item.Name);

See the working sample: https://dotnetfiddle.net/vL6ruv

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194