Home > Software engineering >  How to make a function that returns value from a list in C#
How to make a function that returns value from a list in C#

Time:10-19

I have a list filled with objects. I have to make a function that when given a certain value of one of the class atributes, gives back another one. In my case, when you specify the "index", the function will return "standysp"(I'm sorry if I can't explain it to well). I need to make this function work but I don't even know when to start. Here is my Code:

using System;
using FirebirdSql.Data.FirebirdClient;
using System.Collections.Generic;

namespace dokselect
{
    class PozycjaMagazynowa
    {
        public double standysp;
        public string nazwagrupy;
        public string index;
        public string nazwadl;
    }
    class Program
    {
        public static void Main()
        {

            string conn = "SECRET";
            FbConnection myConnection = new FbConnection(conn);
            FbDataReader myReader = null;

            string sql = "select STANMAG.standysp,GRUPAKART.nazwagrupy, KARTOTEKA.indeks, kartoteka.nazwadl FROM stanmag JOIN kartoteka using(ID_KARTOTEKA) JOIN wystgrkart using(ID_KARTOTEKA) JOIN grupakart using(ID_GRUPAKART) ORDER BY nazwagrupy;";
            FbCommand myCommand = new FbCommand(sql, myConnection);

            myConnection.Open();
            myReader = myCommand.ExecuteReader();

            List<PozycjaMagazynowa> lista1 = new List<PozycjaMagazynowa>();
            double standysp;
            string nazwagrupy;
            string index;
            string nazwadl;
            while (myReader.Read())
            {
                standysp = Convert.ToDouble(myReader[0]);
                nazwagrupy = myReader[1].ToString();
                index = myReader[2].ToString();
                nazwadl = myReader[3].ToString();
                lista1.Add(new PozycjaMagazynowa { standysp = standysp, nazwagrupy = nazwagrupy, index = index, nazwadl = nazwadl });

                
            }
       
            myConnection.Close();
            Console.WriteLine(lista1.Count);

                            //LISTA DONE

            void wyswietl()
            {
                //????????
            }
        }
    }
}

CodePudding user response:

I'm not sure you're doing it the right way. First the List you create is not accessible to others methods. Secund, maybe the List is not appropriate and you may use a HashSet<T> or Dictionnary with your PozycjaMagazynowa class implementing IEqualityComparer

CodePudding user response:

I used this method and it worked thank you for help:

double wyswietl()
        {
            string myIndex="WSIP_MAT_GIM";
            var result = lista1.FirstOrDefault(lista1 => lista1.index == myIndex).standysp;
            Console.WriteLine(myIndex   " Zostalo zwrocone przez funkcje");
            Console.WriteLine(result   " - to stan dyspozycyjny pasujacy do podanego indexu");
            return result;
        }
        wyswietl();
  • Related