ASP.net Looping through a Collection of Dates

M

Mick Walker

I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest
entry, and finishing at the last, and output them in the format:

June 2007
Item
Item
Item
May 2007
Item
Item
....
....
....
....
December 2006
Item
Item
..... and so on.

I have no idea how to achieve this, so if someone could help me that
would be great.
 
G

Guest

I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest
entry, and finishing at the last, and output them in the format:


In your SQL statement, sort the list by date. Then use a datareader (or
dataset) to output the data.
 
C

Cor Ligthert [MVP]

Mick,

Probably I would try to get the Top 1 record, using the Execute Scalar,
order the table by the DateTime and where the date is > than the previous
date.

Avoiding to give you the fish but help you to learn how to fish.

Cor
 
M

Mick Walker

Cor said:
Mick,

Probably I would try to get the Top 1 record, using the Execute Scalar,
order the table by the DateTime and where the date is > than the previous
date.

Avoiding to give you the fish but help you to learn how to fish.

Cor
Thanks for your reply Cor.
However I am still no wiser at the moment. I know what I wish to do, I
am just unsure of how to do it.

I need to loop though my collection,

Process Items Which Are in current Month
Write Heading
Write Items
Process Items Which Are in current Month -1
Write Heading
Write Items
Process Items Which Are in current Month -2
Write Heading
Write Items
Process Items Which Are in current Month -3
Write Heading
Write Items

......


I would like to keep the result set to a max of 6 months.

Any ideas?

Regards
Mick
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mick said:
I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest
entry, and finishing at the last, and output them in the format:

June 2007
Item
Item
Item
May 2007
Item
Item
...
...
...
...
December 2006
Item
Item
.... and so on.

I have no idea how to achieve this, so if someone could help me that
would be great.

Keep track of the year and month, and check against each item that you
output. When it changes, output a new header.
 
M

Mr. Arnold

Mick Walker said:
Thanks for your reply Cor.
However I am still no wiser at the moment. I know what I wish to do, I am
just unsure of how to do it.

I need to loop though my collection,

Process Items Which Are in current Month
Write Heading
Write Items
Process Items Which Are in current Month -1
Write Heading
Write Items
Process Items Which Are in current Month -2
Write Heading
Write Items
Process Items Which Are in current Month -3
Write Heading
Write Items

What do you mean? Are you talking about looping through a collection class
with a collection of objects.

If that's what you're talking about then you use a Foreach loop

Foreach item as theobject in Collection

if item.Date = current Month then
do something
endif

if item.Date = current Month -1 then
do something
endif
next

Or if you're talking about a SQL recordset, then you use a ADO.Net
Datareader and loop through the resultset.

while dr Not EOF
if dr.Date = current Month then
do something
endif
loop

It's really not clear as to what you're asking.
 
C

Cor Ligthert [MVP]

Mick,

Something like this, executed with a command.executescalar seems proper for
me.
(where here the orders from the Northwind database is used)
Select Top 1 OrderDate From Orders Where OrderDate > @previousdate' Order By
OrderDate

Cor
 
S

SteveO

I have a collection of objects populated from a SQL Server (sorted
descending). Each of these object has a date field.

I need a way to loop through these object starting from the newest
entry, and finishing at the last, and output them in the format:

June 2007
Item
Item
Item
May 2007
Item
Item
...
...
...
...
December 2006
Item
Item
.... and so on.

I have no idea how to achieve this, so if someone could help me that
would be great.


Try this:

dim tempDate as item(1).date

do while tempDate < item(n).date
...
tempDate = tempDate.addDays(1)
loop

and so on...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top