1

I have wrote a really messy code, just because I did not had an idea how to loop through the multiple list of objects.

Code is working what it suppose to do, but I am not confident with it. Does someone has an idea how this kind of code can be refactored so it is more readable?

foreach (var outlet in merchant.Outlets)
{
    if (outlet != null)
    {
        foreach (var terminal in merchant.Terminals)
        {
            if (terminal != null)
            {
                foreach (var agreement in terminal.AssociateAgreements)
                {
                    var bankInfo = new Bank();
                    if (agreement != null)
                    {
                        bankInfo.UniqueID = AgreementUniqueCode + agreement.ID;
                        bankInfo.BankBIC = agreement.BankAccountInformation.SwiftBIC;
                        bankInfo.BankName = agreement.BankAccountInformation.BankName;
                        bankInfo.Address =
                            AddressEntityToAddress(agreement.BankAccountInformation.BankAddress,
                                BankingType);

                        bankInfo.type = BankType;
                    }
                    banksAccountInformation.Add(bankInfo);
                }
            }
        }
    }
} 
6
  • 1
    Use methods. You may add a method to Outlet such as ProcessTerminals, and one to Terminal such as ProcessAgreements. Commented May 13, 2016 at 9:16
  • You might want to put this question in 'Code Review' stackoverflow, not really appropriate here. Commented May 13, 2016 at 9:17
  • 1
    You could also remove the if conditions to match with NULL, because foreach loop always itreate on values in the collection and collection doesn't have the NULL insertions. Commented May 13, 2016 at 9:20
  • @Shubhit304: Foreach loops also process null values and collection may contain null values. Therfore the null check is correct. Commented May 13, 2016 at 9:39
  • @JNS: this happens only if the collection is of user defined datatype in which user manages the collection and fill it by values. Other wise I dont think .net will return you any collection having NULL values in between collections total count objects. Commented May 13, 2016 at 9:58

4 Answers 4

4

You could use LINQ, especially SelectMany and Where:

List<Bank> banksAccountInformation = merchant
    .Where(m => m.Outlets != null && m.Terminals != null)
    .SelectMany(m => m.Terminals
        .SelectMany(t => t.AssociateAgreements
            .Where(aa => aa != null)
            .Select(aa => new Bank
            {
                UniqueID = AgreementUniqueCode + aa.ID
                // ...
            })))
    .ToList();
Sign up to request clarification or add additional context in comments.

Comments

3

You could also use the linq query syntax:

var outletQuery = from outlet in merchant.Outlets
                  where outlet != null
                  select outlet;

var agreementQuery = from terminal in merchant.Terminals
                     where terminal != null
                     from agreement in terminal.AssociateAgreements
                     select agreement;

foreach (var outlet in outletQuery)
{
    foreach (var agreement in agreementQuery)
    {
       ProcessAgreement(agreement);
    }
}

Comments

3

If the only thing you want to is drilling down the object hierarchy, then you can use extension methods or linq:

// here you get a flattened collection of Terminals
merchants.SelectMany( m => m.Terminals)
// here you get a flattened collection of all AssociateAgreements of all Terminals
   .SelectMany( t => t.AssociateAgreements)

Comments

0

if i am:

foreach (var outlet in merchant.Outlets)
{
    if (outlet == null)
        continue;

    foreach (var terminal in merchant.Terminals)
    {
        if (terminal == null)
            continue;

        foreach (var agreement in terminal.AssociateAgreements)
        {
            if (agreement == null)
                continue;

            //TODO
        }
    }
}

1 Comment

Please explain the code as well while posting an answer.

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.