Home > Software design >  First element foreach not showing c#
First element foreach not showing c#

Time:02-05

I have an empty List (objPassenger) with passengers spaces incoming, and another list with data of passengers confirmed (objFilePassenger). I can't start from the 0 index in foreach.

Demo in web

foreach (var filePassenger in objFilePassengers)
{
    //TypeId == 1 is ADULT
    //TypeId == 2 is CHILD

    var passenger = objPassengers.FirstOrDefault(p => (
    (p.TypeId == filePassenger.Type && p.TypeId == "1") || (p.TypeId != "1" && filePassenger.Type != "1" && p.Age == filePassenger.Age)));

    if (passenger != null)
    {
        passenger.PassengerId = filePassenger.PassengerID;
        passenger.TypeId = filePassenger.Type;
        passenger.FirstName = filePassenger.Name.ToTitleCase();
        passenger.LastName = filePassenger.LastName.ToTitleCase();
        passenger.Age = filePassenger.Age;
    }
}

How can I skip the object if p.TypeId== 1 && filePassenger.Type == 2? This deferred object I need to skip and go next, until the type is equal like TypeId == 2 and Type == 2 (CHILD).

CodePudding user response:

This is how I solved it. The issue was 2 lists, one with the requested places, and another with the pre-loaded passengers. The first list could contain, for example, 6 places (3 adults and 3 minors) and the second list could have 2 adult passengers and 2 minor passengers. To skip the third adult (or the amount that was), as there is no others adults and the incomming passanger is a minor, it must be left blank. I solved it as follows.

var count = 0;
for (int i = 0; i < objFilePassengers.Count; i  )
{

    if ((objFilePassengers[i].Type == "1" && objPassengers[i].TypeId == "1") || (objPassengers[i].TypeId == "2" && objFilePassengers[i].Type == "2" && objFilePassengers[i].Age < 18))
    {
        objPassengers[i].PassengerId = objFilePassengers[i].PassengerID;
        objPassengers[i].TypeId = objFilePassengers[i].Type;
        objPassengers[i].FirstName = objFilePassengers[i].Name.ToTitleCase();
        objPassengers[i].LastName = objFilePassengers[i].LastName.ToTitleCase();
        objPassengers[i].Age = objFilePassengers[i].Age;
    }
    else if ((objPassengers[i].TypeId == "1" && objFilePassengers[i].Type == "2"))
    {
        objFilePassengers.Insert(count   1, new clsEnix_Passenger
        {
            PassengerID = objPassengers[i].PassengerId,
            Name = "",
            LastName = "",

        });
        count  ;
    }
}

Example result

CodePudding user response:

Updated Answer

Have you considered using a custom Collection to hold your passengers? You can build all the logic into the collection and make it enumerable to iterate through the passengers.

Here's a basic example built from your code:

Two data objects to represent a passenger. I use records because they make equality checks and cloning easy, and you get immutability. Just use the class object when you want to edit the record.

public record PassengerRecord(Guid PassengerId, string PassengerName, int PassengerType);

public class Passenger
{
    public Guid PassengerId { get; set; } = Guid.NewGuid();
    public string PassengerName { get; set; } = "Not Set";
    public int PassengerType { get; set; }
    public PassengerRecord AsRecord => new(PassengerId, PassengerName, PassengerType);

    public Passenger() { }
    public Passenger(PassengerRecord record)
    {
        this.PassengerId = record.PassengerId;
        this.PassengerName = record.PassengerName;
        this.PassengerType = record.PassengerType;
    }
}

And then a very basic PassengerList which implements IEnumerable, restricts the number of passengers and checks if they are already on the list. Build your own logic and functionality into this.

public class PassengerList : IEnumerable<PassengerRecord>
{
    private List<PassengerRecord> _items = new List<PassengerRecord>();
    private int _maxRecords;

    public PassengerList(int maxRecords)
        => _maxRecords = maxRecords;

    public bool TryAddItem(Passenger passenger)
        => addItem(passenger.AsRecord);

    public bool TryAddItem(PassengerRecord passenger)
        => addItem(passenger);

    public bool addItem(PassengerRecord passenger)
    {
        // Doi whatever checks you want to do
        if (!_items.AsQueryable().Any(item => item == passenger) && _items.Count < _maxRecords)
            _items.Add(passenger);

        return true;
    }

    public PassengerRecord this[int index]
        => _items[index];

    public IEnumerator<PassengerRecord> GetEnumerator()
        => _items.GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator()
        => this.GetEnumerator();
}
  • Related