How to get an Array of items contained in a List

willowha

I have to find the names passing through the parameter array in the List with lambda. Should I have to iterate for searching equal names from the array?

 partial class StudentTestScoresProgram
    {
        static public List<Student> GetStudents()
        {
            List<Student> students = new List<Student>
            {
                new Student { Name = "Michael", Scores = new int[] { 94, 92, 91, 91 } },
                new Student { Name = "Isabelle", Scores = new int[] { 66, 87, 65, 93, 86} },
                new Student { Name = "Chastity", Scores = new int[] { 76, 61, 73, 66, 54} },
                new Student { Name = "Chaim", Scores = new int[] { 94, 55, 82, 62, 52} },
                new Student { Name = "Patience", Scores = new int[] { 91, 79, 58, 63, 55} },
                new Student { Name = "Echo", Scores = new int[] { 74, 85, 73, 75, 86} },
                new Student { Name = "Pamela", Scores = new int[] { 73, 64, 53, 72, 68} },
                new Student { Name = "Anne", Scores = new int[] { 78, 96, 52, 79, 60} },
                new Student { Name = "Bruno", Scores = new int[] { 62, 66, 70, 82, 74} },
                new Student { Name = "Tanya", Scores = new int[] { 60, 77, 88, 99, 40} }

 };

            return students;
        }

  /// <summary>
        /// Find a set of students and display their information
        /// 
        /// You MUST use lambda or Predicate/delegate with FindAll for this.
        /// </summary>
        /// <param name="students">list of students</param>
        /// <param name="search">array of student names to find</param>
        static void FindStudents(List<Student> students, params string[] search)
        {
            //I have to fill the code in here. 
            List<Student> subArray = students.FindAll(i => i.Name == search[]); //This is what I was thinking of
        }

I have to fill the code, how can I search for an array?

The program is

 static void Main(string[] args)
        {
            List<Student> students = GetStudents();
            FindStudents(students, "Bruno", "Tanya", "Tony", "Sami");
            FindStudents(students, "Xerxes");
        }

And the output should be like this

Searching for the following students:
Bruno
Tanya
Tony
Sami
Found the following students:
Name: Bruno, Scores: 62, 66, 70, 82
Name: Tanya, Scores: 60, 77, 88, 99
Searching for the following students:
Xerxes
Found the following students:
00110001

I am only going to focus on the actual Lambda. Matching the exact output results is up to you

However, you can just use Contains

Determines whether a sequence contains a specified element.

static List<Student> FindStudents(List<Student> students, params string[] search)
    => students.FindAll(i => search.Contains(i.Name));

Or if you want a fast case insensitive search you could use a HashSet with a StringComparer

static void FindStudents(List<Student> students, params string[] search)
{
    var hash = search.ToHashSet(StringComparer.InvariantCultureIgnoreCase);
    return students.FindAll(i => hash.Contains(i.Name));
}

Example

var students = GetStudents();
var results = FindStudents(students, "Pamela", "Anne", "Tony", "Sami");

foreach (var result in results)
   Console.WriteLine($"{result.Name} : {string.Join(",", result.Scores)}");

Output

Pamela : 73,64,53,72,68
Anne : 78,96,52,79,60

Note : These methods lack suitable input checking

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the length of an Array that is contained within an Object?

From Dev

How to get the visible list items count in a Xamarin android Array adapter

From Dev

How to get a list of all subobjects contained in a list of objects?

From Dev

How to List items in array javascript

From Dev

How to get a list of similar items

From Dev

How to pass a list of parameters contained in an array to bind_param?

From Dev

How to get n elements of a list not contained in another one?

From Dev

Linq query not including items contained in list (of class)

From Dev

Error removing items contained in array from listbox

From Dev

How to store list items within an array with jQuery

From Dev

How to cleanly list items in an array - Mongoose

From Dev

How to match list items with values in an array

From Dev

How to cleanly list items in an array - Mongoose

From Dev

How to determine the height of all items contained in a DockPanel

From Dev

How to get all items in multi list in bottle?

From Dev

How to get list items by index in freemarker template?

From Dev

How to get evenly spaced list items?

From Dev

How to get the list of items from the p tags

From Dev

Elixir: How to get last n items in a list?

From Dev

How to get rid of the dots on the side of list items?

From Dev

How to get a single list of items from dataframe

From Dev

How to get evenly spaced list items?

From Dev

how to get the id of sub-items of the list?

From Dev

How to get all items in multi list in bottle?

From Dev

How to get the list of items in a transaction in package arules

From Dev

How to get list of Items with ids in NHibernate

From Dev

How to get "Repro Steps" of a list of work items?

From Dev

how to get list items always same height?

From Dev

How to get sequential permutation from a list of items?

Related Related

  1. 1

    How to get the length of an Array that is contained within an Object?

  2. 2

    How to get the visible list items count in a Xamarin android Array adapter

  3. 3

    How to get a list of all subobjects contained in a list of objects?

  4. 4

    How to List items in array javascript

  5. 5

    How to get a list of similar items

  6. 6

    How to pass a list of parameters contained in an array to bind_param?

  7. 7

    How to get n elements of a list not contained in another one?

  8. 8

    Linq query not including items contained in list (of class)

  9. 9

    Error removing items contained in array from listbox

  10. 10

    How to store list items within an array with jQuery

  11. 11

    How to cleanly list items in an array - Mongoose

  12. 12

    How to match list items with values in an array

  13. 13

    How to cleanly list items in an array - Mongoose

  14. 14

    How to determine the height of all items contained in a DockPanel

  15. 15

    How to get all items in multi list in bottle?

  16. 16

    How to get list items by index in freemarker template?

  17. 17

    How to get evenly spaced list items?

  18. 18

    How to get the list of items from the p tags

  19. 19

    Elixir: How to get last n items in a list?

  20. 20

    How to get rid of the dots on the side of list items?

  21. 21

    How to get a single list of items from dataframe

  22. 22

    How to get evenly spaced list items?

  23. 23

    how to get the id of sub-items of the list?

  24. 24

    How to get all items in multi list in bottle?

  25. 25

    How to get the list of items in a transaction in package arules

  26. 26

    How to get list of Items with ids in NHibernate

  27. 27

    How to get "Repro Steps" of a list of work items?

  28. 28

    how to get list items always same height?

  29. 29

    How to get sequential permutation from a list of items?

HotTag

Archive