1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

foreach loop question

Discussion in 'Software' started by jcast, May 25, 2012.

  1. jcast

    jcast Private E-2

    I have this foreach statement to filter from my list of address (addressinfo) the ones that doesnt have the flag deleted on DB.

    var accountSvc = new AccountSvc();
    var personInfo = accountSvc.GetProfileInfo(AccountPhone);
    var addressInfo = accountSvc.GetPersonAddresses(personInfo.Id);

    if (repAddresses != null)
    foreach (var add in addressInfo)
    if (add.Deleted == false || add.Deleted == null)
    {
    var personSvc = new Shared.Core.Person.PersonSvc();
    var address = personSvc.GetAddressProfile(add.AddressId);
    // addressInfo = address;
    }

    repAddresses.DataSource = addressInfo;
    repAddresses.DataBind();

    if I do addessInfo = address I will get only the last address in loop, how can I save each address that go thru the loop and return them all.

    Thanks
     
  2. Wyatt_Earp

    Wyatt_Earp MajorGeek

    You can use Linq/Lambda Expressions (if you are using .NET 4.0)

    Code:
    var activeAddresses = addressInfo.Where(add => add.Deleted == false || add.Deleted == null)
    
    foreach (add in activeAddresses)
    {
       // Do Processing on the addresses
    }
    
    If you aren't using .NET 4.0, then you can create a new list containing only the active addresses as you loop through.

    Code:
    List<AddressInfoType> activeAddresses = new List<AddressInfoType>(); //*
    
    foreach (var add in addressInfo)
    {
      if (add.Deleted == false || add.Deleted == null)
      {
        var personSvc = new Shared.Core.Person.PersonSvc();
        var address = personSvc.GetAddressProfile(add.AddressId);
        
        activeAddresses.Add(address);
      }
    }
    
    *Note - Replace AddressInfoType with the actual DataType of addressInfo.
     
  3. jcast

    jcast Private E-2

    Many Thanks to Wyatt_Earp
     
  4. vincelegard

    vincelegard Private E-2

Share This Page

MajorGeeks.Com Menu

MajorGeeks.Com \ All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ NEW! PC Games \ System Tools \ Macintosh \ Demonews.Com \ Top Downloads

MajorGeeks.Com \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds