Home > database >  Simple program asking query Array in list
Simple program asking query Array in list

Time:10-21

I have a little program about query the list by using linq in C# programming.

The is the class

public class Student
{
    public string studentname { get; set; }
    public int[] CardNumber { get; set; }
}

This is how I create the static list

public static IList<Student> StudentConfig = new List<Student>
        {
            new() { studentname="Peter", CardNumber= new int[] {2 } },
            new() { studentname= "Winnie" , CardNumber= new int[] {3}  },
            new() { studentname = "Gilbert", CardNumber = new int[] { 2,3} }
        };

The challenge is to write a simple function based on simple config to find out which student name. The element card number can be multiply by input value.

The function as shown here:

public static string  MultiplyListCheckUp (int InputValue)
{
    // For example InputValue : 4 , then return  studentname="Peter" , as 4 is only factor of 2
    // For example InputValue : 9 , then return  studentname="Winnie" , as 9 is only factor of 3
    // For example InputValue : 6 , then return  studentname="Gilbert", as 6 is factor of 2 and 3

    return string.Empty;
 } 

About studentconfig can be adjustable. E.g. we can add new record

studentname = "Sally", CardNumber = new int[] { 2,5} 

If input value : 10, it will return Sally

But, if input value : 30 which have conflict more than 1 rules, it will return "multi records occur" or throw exception

Can anyone solve it ?

Thank you

CodePudding user response:

I'm not sure you properly described question, but in condition of:

// For example:
// if inputValue 4, then return "Peter"
// if inputValue 9, then return "Winnie"
// if inputValue 6, then return "Gilbert"
// if inputValue 10, then return "Sally"
// otherwise - show error or throw exception

this example should work:

public class Student
{
    public string Name { get; set; }
    public int[] CardNumber { get; set; }
}

class Program
{
    public static IList<Student> StudentConfig = new List<Student>
    {
        new() { Name="Peter",     CardNumber = new int[] { 2 }        },
        new() { Name= "Winnie" ,  CardNumber = new int[] { 3 }        },
        new() { Name = "Gilbert", CardNumber = new int[] { 2, 3 }     },
        new() { Name = "Sally",   CardNumber = new int[] { 2, 5 }     },
        new() { Name = "Bob",     CardNumber = new int[] { 3, 6, 8 }  },
        new() { Name = "Jack",    CardNumber = new int[] { 5, 7, 12 } }
    };

    static void Main()
    {
        Console.WriteLine("- Students by CardNumber selector -\n");

        while (true)
        {
            Console.Write("Your input: ");
            if (!int.TryParse(Console.ReadLine(), out var inputNumber))
            {
                Console.Write("Invalid number was inputed. Want to try again? (Y/N): ");
                if (Console.ReadLine().ToUpper() != "Y")
                    break;
                else
                    continue;
            }

            var foundedStudents = StudentConfig.Where(student => (student.CardNumber.Length > 1
                                                                  ? student.CardNumber.Aggregate(1, (a, b) => a * b)
                                                                  : student.CardNumber[0] * student.CardNumber[0])
                                                                  == inputNumber);
            var count = foundedStudents.Count();

            switch (count)
            {
                case 1:
                    Console.WriteLine("Founded Student: "   foundedStudents.First().Name);
                    break;
                case 0:
                    Console.WriteLine("There are no records at result with provided number");
                    break;
                case > 1:
                    Console.WriteLine("There are multiple records at result with provided number");
                    break;
                default:
                    Console.WriteLine("How it is possible to get here?");
                    break;
            }
        }

        Console.WriteLine("\nProgram complete. Press any key to close console...");
        Console.ReadKey();
    }
}

So I used a ternary, which checks length of Student's CardNumber array and multiplies each array value if .Length > 1 using .Aggregate() or multiplies value by itself. Ternary result compares with user's input and on compare result .Where() method filters StudentConfig collection.

After filtering - check how many elements was retrieved and, depending on your conditions, if only 1 single Student - print in, if nothing found or more than 1 - show error.

All that wrapped into while loop until user input something, but not a number.

Result looks like that: enter image description here

  • Related