MajorGeeks Support Forums

Go Back   MajorGeeks Support Forums > ----------= PC, Desktop and Laptop Support =---------- > Software
Register FAQ Members List Calendar Casino Mark Forums Read

Software Software such as operating systems like Windows XP, Windows Vista, Windows 7 etc., or specific programs.


Reply
 
Thread Tools Display Modes
  #1  
Old 05-25-12, 09:38
jcast jcast is offline
Private E-2
 
Join Date: Apr 2012
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default foreach loop question

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
Reply With Quote
Sponsored links
  #2  
Old 05-25-12, 17:53
Wyatt_Earp's Avatar
Wyatt_Earp Wyatt_Earp is offline
Major Geek
 
Join Date: Sep 2002
Location: Powell, OH
Posts: 1,377
Thanks: 3
Thanked 26 Times in 23 Posts
Default Re: foreach loop question

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.
__________________
"I cannot undertake to lay my finger on that article of
the Constitution which granted a right to Congress of
expending, on the objects of benevolence, the money
of their constituents."

-- James Madison
(1751-1836), Father of the Constitution for the USA,
4th US President
_________________________
AMD Athlon 64 3800+
ABIT KN8 SLI
2 Gb (2x1Gb) PC 3200 Cas 2 Corsair XMS RAM
2x120Gb WD w/ 8Mb cache (RAID 0)
eVGA GeForce 7900GT 256MB
Reply With Quote
The Following User Says Thank You to Wyatt_Earp For This Useful Post:
jcast (05-30-12)
  #3  
Old 05-30-12, 09:38
jcast jcast is offline
Private E-2
 
Join Date: Apr 2012
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default Re: foreach loop question

Many Thanks to Wyatt_Earp
Reply With Quote
  #4  
Old 06-26-12, 01:06
vincelegard vincelegard is offline
Private E-2
 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: foreach loop question

The foreach loop is useful for traversing each items in an array or a collection of items and displayed one by one.

for each syntax is

foreach(variable type in collection){
// code block
}

source : http://csharp.net-informations.com/s...p-for-loop.htm

legard.
Reply With Quote
Reply

Tags
csharp

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Out of the loop DxH Hardware 6 04-07-11 19:56
VBA loop Black_Bean Software 0 11-09-10 17:09
Log in/log off loop epicfail13 Malware Removal 4 06-12-09 14:28
Log in/log off loop epicfail13 Software 8 06-06-09 21:07
do..while loop question - help newbie please bobjohnson Software 1 07-05-04 19:26


All times are GMT -5. The time now is 23:02.

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


All content Copyright MajorGeeks.com source code Powered by vBulletin® Version 3.8.4
Copyright © 2009 vBulletin Solutions, Inc. All rights reserved.
Ad Management by RedTyger