Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't figure out how to check if list contains 0

this code checks if the list of counterparties contains email addresses. then in the else statement there is a chance that the email address still becomes 0. I need a piece of code that fills in the email address list when it is zero.

if (counterParty == null)
{
    mailAddressesOfCounterparty = new List<Email>();
    Email unKnownEmail = new Email();
    unKnownEmail.EmailAddress = loopPayment.ShortNameCalypso + "@NotInCounterpartyTable.nl";
    mailAddressesOfCounterparty.Add(unKnownEmail);
}
else
{
    mailAddressesOfCounterparty =
        emailAddress.Where(ea => ea.CounterPartyId == counterParty.Id && ea.IsOptionContract == startOfGroupPayment.OptionContract).ToList();                        
}

this code needs to make the email address. only don't know how to check if zero.

Email unKnownEmail = new Email();
unKnownEmail.EmailAddress = loopPayment.ShortNameCalypso + "@NotInCounterpartyTable.nl";
mailAddressesOfCounterparty.Add(unKnownEmail);

in the else i need to add a possibility to change the email address to something when it becomes zero there. the code won't let me us an if statement.

mailAddressesOfCounterparty becomes zero because something isn't added in the database yet. But that information could be missing when using this application aswell. In that case i want to create an emailaddress that will show that it couldn't be found.

like image 532
Petah Avatar asked Mar 14 '23 01:03

Petah


2 Answers

If I understand the question correctly, you want to verify whether there are any addresses in the list which you've filtered in the else clause.

The linq extension method "Any()" can be used to determine whether an enumeration contains any items (there's other ways too), an example of this in use (based on your code) is:

if (counterParty == null)
{
    mailAddressesOfCounterparty = new List<Email>();
    Email unKnownEmail = new Email();
    unKnownEmail.EmailAddress = loopPayment.ShortNameCalypso + "@NotInCounterpartyTable.nl";
    mailAddressesOfCounterparty.Add(unKnownEmail);
}
else
{
    mailAddressesOfCounterparty = emailAddress.Where(ea => ea.CounterPartyId == counterParty.Id && ea.IsOptionContract == startOfGroupPayment.OptionContract).ToList();

    if (!mailAddressesOfCounterparty.Any()) 
    {
        Email unKnownEmail = new Email();
        unKnownEmail.EmailAddress = loopPayment.ShortNameCalypso + "@NotInCounterpartyTable.nl";
        mailAddressesOfCounterparty.Add(unKnownEmail);
    }
}
like image 74
David_001 Avatar answered Mar 17 '23 07:03

David_001


If I understood the question correctly, in the else branch you could introduce an initialization like the following

mailAddressesOfCounterparty = mailAddressesOfCounterparty ?? new List<Email>();

which would initialize it with a new list if it is null. However, I don't see how mailAddressesOfCounterparty could be null after the first statement of the else branch.

like image 23
Codor Avatar answered Mar 17 '23 08:03

Codor