Storing SQL data in a C# List and finding the maximum value without LINQ

I’m trying to figure out how to store data from SQL in a C# List. I want to save the data from a class instance in the List and then find the maximum value. Here’s what I’ve got so far:

public class PrinterData
{
    public static PrinterInfo FetchPrinterInfo()
    {
        PrinterInfo printerInfo = new PrinterInfo();

        using (SqlConnection conn = DatabaseConnection.GetPrinterInfoFromDB())
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM PrinterQueue WHERE QueuedTime >= DATEADD(day, -2, GETDATE())", conn);

            using (SqlDataReader dataReader = cmd.ExecuteReader())
            {
                if (dataReader.Read())
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine($"{dataReader.GetName(0)}\t{dataReader.GetName(1)}\t{dataReader.GetName(4)}\t{dataReader.GetName(5)}");
                    while (dataReader.Read())
                    {
                        printerInfo.ID = (int)dataReader["ID"];
                        printerInfo.Name = (string)dataReader["Name"];
                        printerInfo.QueuedTime = (DateTime)dataReader["QueuedTime"];
                        printerInfo.LastUpdate = (DateTime)dataReader["LastUpdate"];
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine($"{dataReader.GetSqlInt32(0)}\t{dataReader.GetSqlString(1)}\t{dataReader.GetSqlDateTime(4)}\t{dataReader.GetSqlDateTime(5)} = {printerInfo.TimeDifference}sec");
                        Console.WriteLine("----------------------------");
                    }
                    Console.ReadKey();
                    return printerInfo;
                }
                return null;
            }
        }
    }
}

Can someone help me store this data in a List and find the max value? Thanks!

hey there! i’m curious about your project. have you considered using a Dictionary instead of a List? it might be easier to work with for finding max values. what kind of printer data are you dealing with? maybe we could brainstorm some creative solutions togehter!

i got u. return a List in your method. inside the while loop, create a new PrinterInfo for each record and add it to the list. then, loop over the list to find the max TimeDifference. no need for LINQ, trust me.

To store the data in a List and find the maximum value, you can modify your code as follows:

  1. Create a List at the beginning of the FetchPrinterInfo method.

  2. Inside the while loop, create a new PrinterInfo object for each row and add it to the list.

  3. After the loop, use a simple foreach to find the maximum TimeDifference.

Here’s a simplified example:

public static List<PrinterInfo> FetchPrinterInfo()
{
    List<PrinterInfo> printerInfoList = new List<PrinterInfo>();

    using (SqlConnection conn = DatabaseConnection.GetPrinterInfoFromDB())
    {
        // ... (your existing code)

        while (dataReader.Read())
        {
            PrinterInfo printerInfo = new PrinterInfo
            {
                ID = (int)dataReader["ID"],
                Name = (string)dataReader["Name"],
                QueuedTime = (DateTime)dataReader["QueuedTime"],
                LastUpdate = (DateTime)dataReader["LastUpdate"]
            };
            printerInfoList.Add(printerInfo);
        }
    }

    return printerInfoList;
}

// To find max TimeDifference
PrinterInfo maxPrinter = printerInfoList[0];
foreach (var printer in printerInfoList)
{
    if (printer.TimeDifference > maxPrinter.TimeDifference)
    {
        maxPrinter = printer;
    }
}

Console.WriteLine($"Max TimeDifference: {maxPrinter.TimeDifference}");

This approach avoids LINQ and uses basic C# constructs to achieve your goal.