1

I hope this isn't a foolishly simple question. Im very simply trying to figure out how to manipulate a relatively simple table in SQLite through C#.

Im looking to take a parameter and search a List of Arrays for one such array where the parameter matches, and return a related variable within that same array.

For example where an array in the list might be.

Name          IATA

Brisbane        BNE

The sqlbind:

public static List<Airport> LoadAirports()
{
    using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
    {
        var output = cnn.Query<Airport>("select * from Airport", new DynamicParameters());
        return output.ToList();
    }
}

The Class:

class Airport
{
    int Id { get; set; }
    string Name { get; set; }
    string LocationName { get; set; }
    string IATA { get; set; }
    string PortType { get; set; }
    string PortOwner { get; set; }
    string MotherPort { get; set; }
    bool Active { get; set; }
    bool IsApplyMeetAndGreet { get; set; }
    decimal MeetAndGreet { get; set; }
}

The main Program:

List<Airport> Airports = new List<Airport>();
public FreightCalculator()
{
    LoadAirportsList();
    string OriginName = OriginInput.Value;
    var OriginAirport = Airports.Where(s => s.Name == OriginName);
}
private void LoadAirportsList()
{
    Airports = SqliteDataAccess.LoadAirports();
}

Ive tried various combinations of Where, Equals, For each indexing etc. Always getting an error of some kind.

The Error with the above Airports.Where is that the s.Name is inaccessible due to its protection level.

If I do:

var OriginAirport = Airports.Where(Name => Name == OriginName);

I get an error where the operand == cannot be used with Airport and String (Though Name is a string in Airport.)

Im either missing something simple or making this more complicated than it needs to be. Once I find the matching Airport, I need to return the IATA code.

Which I envisage looking like this:

var OriginIATA = OriginAirport.IATA;

Im tired and feeling dumb. Please help :(

10
  • 2
    Currently all members of the Airport class are private, therefore you cannot access them outside of the Airport class. Declare them public and check if it helps you. For example, public string Name { get; set; }. Also check this link: access modifiers. Commented Jun 2, 2020 at 4:27
  • @IliarTurdushev Thanks for your input, that does seem to fix the error in the Airports.Where section, but then i get this error for the OriginIata = section. "Error CS1061 'IEnumerable<Airport>' does not contain a definition for 'IATA' and no accessible extension method 'IATA' accepting a first argument of type 'IEnumerable<Airport>' could be found (are you missing a using directive or an assembly reference?)" Commented Jun 2, 2020 at 4:46
  • It is another issue in your code. Method Where returns IEnumerable (collection of objects). If you are sure that there must be only one Airport with specified Name, then you should also include a call to the method FirstOrDefault (or SingleOrDefault) to return the first element of the collection returned by the method Where: var OriginAirport = Airports.Where(s => s.Name == OriginName).FirstOrDefault();. And then you can use: var OriginIATA = OriginAirport.IATA;. Commented Jun 2, 2020 at 4:53
  • Like this? [var OriginAirport = Airports.SingleOrDefault(s => s.Name == OriginName); var OriginIATA = OriginAirport.IATA;] Seems to function! Can you post an answer so i can credit you for helping? :) Commented Jun 2, 2020 at 4:58
  • Yes, like this. But you should take into account that method SingleOrDefault throws an exception if it does not find an element of finds more than one element that satisfies specified condition. Please, check its documentation. Commented Jun 2, 2020 at 4:58

1 Answer 1

1

Since you declared all members of the Airport class as properties I assume you wanted to expose them publicly.

The error you get is because they are private members and can't be accessed outside the class.

Change "Airport" class to:

class Airport
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string LocationName { get; set; }
    public string IATA { get; set; }
    public string PortType { get; set; }
    public string PortOwner { get; set; }
    public string MotherPort { get; set; }
    public bool Active { get; set; }
    public bool IsApplyMeetAndGreet { get; set; }
    public decimal MeetAndGreet { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Soborg, this fixes the first issue of protected variable. Thankyou very much, as well as Iliar Turdushev's response. Still stuck on the Second issue though. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.