I had to switch my code from being in .NET Core to .NET Framework and now I get an error. the error says that local variable named 'listadb' cannot be declared in this scope. This function worked before and I don't know how to get around it now. Here is the code:
using System;
using FirebirdSql.Data.FirebirdClient;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace dokselect
{
class indexstan
{
public string index;
public double standysp;
}
public class WynikPorownania
{
public string Indeks;
public int Ilosc;
public override string ToString()
{
return Indeks " : " Ilosc;
}
}
class Program
{
public static List<WynikPorownania> funkcjalista()
{
///////CONNECTION
string conn = "CONNECTION STRING";
FbConnection myConnection = new FbConnection(conn);
FbDataReader myReader = null;
string sql = "select KARTOTEKA.indeks,STANMAG.standysp FROM kartoteka INNER JOIN stanmag using(ID_KARTOTEKA) WHERE stanmag.ID_MAGAZYN=10002;";
FbCommand myCommand = new FbCommand(sql, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
///////LIST lista1
List<indexstan> listadb = new List<indexstan>();
double standysp;
string index;
while (myReader.Read())
{
index = myReader[0].ToString();
standysp = Convert.ToDouble(myReader[1]);
if (standysp < 0)
{
standysp = 0;
}
listadb.Add(new indexstan { index = index, standysp = standysp });
//Console.WriteLine(myReader[0].ToString());
}
myConnection.Close();
//Console.WriteLine(listadb.Count);
//RETURN STANDYSP FUNCTION
double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex)?.standysp;
return result ?? -1;
}
//READ FROM TXT AND RETURN HIGHER
string path = "C:/Users/Praktykant/Documents/textdocs/dok1.txt";
List<WynikPorownania> listatf = File.ReadAllLines(path).Select(line =>
{
var linia = line.Split("=");
string index = linia[0];
int value = -1;
try
{
value = int.Parse(linia[1]);
}
catch (IndexOutOfRangeException)
{
index = " - ZLE ZAPISANA LINIA";
}
catch (FormatException)
{
index = " - PODANO ZLA WARTOSC";
}
if (zwr(index) == -1)
{
return new WynikPorownania { Indeks = index, Ilosc = (int)zwr(index) };
}
else
{
if (value < 0)
{
value = 0;
}
return new WynikPorownania { Indeks = index, Ilosc = (int)Math.Max(value, zwr(index)) };
}
}).ToList();
//DISPLAY ALL LISTATF CLASSES
foreach (WynikPorownania WynikPorownania in listatf)
{
Console.WriteLine(WynikPorownania);
}
return listatf;
}
public static void Main()
{
funkcjalista();
}
}
}
And here is the function where the error occures(the program underlines listadb after FirstOrDefault:
//RETURN STANDYSP FUNCTION
double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex)?.standysp;
return result ?? -1;
}
Thanks for help
CodePudding user response:
var result = listadb.FirstOrDefault(listadb => listadb.index ^^^^^^^ ^^^^^^^
Don't call the parameter to the lambda the same as the list variable
double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(ldbitem => ldbitem.index == myIndex)?.standysp;
return result ?? -1;
}
(I changed listadb => listadb.index
to ldbitem => ldbitem.index
) - generally it's probably a more sensible naming practice anyway, for example if you had a list of cars, then the delegate parameter would be a car, because it's a single car being passed to the delegate, not multiple cars:
cars.ForEach(car => ...)