none
Avoiding foreach with Linq RRS feed

  • Question

  • Hi All,

    I have below function using three loops. Could someone guide me to use Linq to replace them please .

    public  static bool CheckAnyAddress(this AccountsAccount account)
            {           

    if (account == null) return false;          
    var checkList = new[] { "text1", "text2", "text3" };
    var addressEntries = account.Persons.Where(a => a.PersonAddresses != null);
    foreach (var s in addressEntries)
    {
    foreach (var t in s.PersonAddresses)
     {
    foreach (var item in checkList)
    {
    if (((s.FirstName != null && s.FirstName.Contains(item)) || (s.Surname != null && s.Surname.Contains(item)))
    || ((t.PersonAddress1 != null && t.PersonAddress1.Contains(item))
    || (t.PersonAddress2 != null && t.PersonAddress2.Contains(item))
    || (t.PersonAddress3 != null && t.PersonAddress3.Contains(item))
    || (t.PersonAddress4 != null && t.PersonAddress4.Contains(item))
    || (t.PersonAddress5 != null && t.PersonAddress5.Contains(item))
    || (t.PostCode != null && t.PostCode.Contains(item))))
     return true;
    }
    }
    }
    return false;
    }

    Friday, July 3, 2015 11:14 AM

Answers

  • Hi,

    I had created class based on the conditions you had specified, and added the LINQ query instead of for loops.

    public static bool CheckAnyAddress(AccountsAccount account)
            {
    
                if (account == null) return false;
                var checkList = new[] { "text1", "text2", "text3" };
                var addressEntries = account.Persons.Where(a => a.PersonAddresses != null);
                return addressEntries.Any(s => s.PersonAddresses.Any(t => checkList.Any(item => ((s.FirstName != null && s.FirstName.Contains(item)) 
                                                                                                  || (s.Surname != null && s.Surname.Contains(item)))
                                                                                                || ((t.PersonAddress1 != null && t.PersonAddress1.Contains(item))
                                                                                                    || (t.PersonAddress2 != null && t.PersonAddress2.Contains(item)) 
                                                                                                    || (t.PersonAddress3 != null && t.PersonAddress3.Contains(item)) 
                                                                                                    || (t.PersonAddress4 != null && t.PersonAddress4.Contains(item)) 
                                                                                                    || (t.PersonAddress5 != null && t.PersonAddress5.Contains(item)) 
                                                                                                    || (t.PostCode != null && t.PostCode.Contains(item))))));
            }
    
            public class AccountsAccount
            {
                public List<Person> Persons { get; set; } 
            }
    
            public class Person
            {
                public string FirstName { get; set; }
                public string Surname { get; set; }
                public List<PersonAddress> PersonAddresses { get; set; }
            }
    
            public class PersonAddress
            {
                public string PersonAddress1 { get; set; }
                public string PersonAddress2 { get; set; }
                public string PersonAddress3 { get; set; }
                public string PersonAddress4 { get; set; }
                public string PersonAddress5 { get; set; }
                public string PostCode { get; set; }
            }

    Feel free to ask if you need any clarifications.

    Please mark the post as answer if it is helpfull to you - Hiran Repakula

    • Proposed as answer by Fred BaoModerator Monday, July 6, 2015 2:34 AM
    • Marked as answer by Jerry321 Wednesday, July 8, 2015 1:37 PM
    Friday, July 3, 2015 2:51 PM

All replies

  • Hi,

    I had created class based on the conditions you had specified, and added the LINQ query instead of for loops.

    public static bool CheckAnyAddress(AccountsAccount account)
            {
    
                if (account == null) return false;
                var checkList = new[] { "text1", "text2", "text3" };
                var addressEntries = account.Persons.Where(a => a.PersonAddresses != null);
                return addressEntries.Any(s => s.PersonAddresses.Any(t => checkList.Any(item => ((s.FirstName != null && s.FirstName.Contains(item)) 
                                                                                                  || (s.Surname != null && s.Surname.Contains(item)))
                                                                                                || ((t.PersonAddress1 != null && t.PersonAddress1.Contains(item))
                                                                                                    || (t.PersonAddress2 != null && t.PersonAddress2.Contains(item)) 
                                                                                                    || (t.PersonAddress3 != null && t.PersonAddress3.Contains(item)) 
                                                                                                    || (t.PersonAddress4 != null && t.PersonAddress4.Contains(item)) 
                                                                                                    || (t.PersonAddress5 != null && t.PersonAddress5.Contains(item)) 
                                                                                                    || (t.PostCode != null && t.PostCode.Contains(item))))));
            }
    
            public class AccountsAccount
            {
                public List<Person> Persons { get; set; } 
            }
    
            public class Person
            {
                public string FirstName { get; set; }
                public string Surname { get; set; }
                public List<PersonAddress> PersonAddresses { get; set; }
            }
    
            public class PersonAddress
            {
                public string PersonAddress1 { get; set; }
                public string PersonAddress2 { get; set; }
                public string PersonAddress3 { get; set; }
                public string PersonAddress4 { get; set; }
                public string PersonAddress5 { get; set; }
                public string PostCode { get; set; }
            }

    Feel free to ask if you need any clarifications.

    Please mark the post as answer if it is helpfull to you - Hiran Repakula

    • Proposed as answer by Fred BaoModerator Monday, July 6, 2015 2:34 AM
    • Marked as answer by Jerry321 Wednesday, July 8, 2015 1:37 PM
    Friday, July 3, 2015 2:51 PM
  • Thank you :)  That helped me a lot
    Wednesday, July 8, 2015 1:39 PM