473,499 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lookback logic in a dataset

Joe
I have a dataset as below. I want to iterate through the dataset
comparing the value of the Code column to the value in the previous
row. If they are the same then I want to concatenate the value of the
Desc column in my current row to that of the value in the previous row.
If the value of Code is different to the value in the previous row then
I want to output the row to a dataset. I managed to achieve this
functionality but in a very messy way I'm sure there must be a more
elegant solution to what I have come up with.

Can anyone come up with a simple algorithm to solve this?

Input dataset

ItemNum Code Desc
1 A Apple
2 A Cider
3 A Scrumpy
4 B Strawberry

Resulting dataset
Code Desc
A Apple Cider Scrumpy
B Strawberry
Joe

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #1
4 1107
My example assumes that you are the data store in a table called "Table1"

string prevCode;
DataTable table = ds.Tables["Table1"];
DataRow row = table.Rows[0];
prevCode = (string)row["Code"];
for(int i = 1; i < table.Rows.Count; i++)
{
row = table.Rows[i];
if(row["Code"] == prevCode)
{
(table.Rows[i-1])["Desc"] = String.Concat((table.Rows[i-1])["Desc"],
row["Desc"]);
table.Rows.RemoveAt(i);
i -= 1;
}
}

hope it helps,
Ivan Wong

"Joe" wrote:
I have a dataset as below. I want to iterate through the dataset
comparing the value of the Code column to the value in the previous
row. If they are the same then I want to concatenate the value of the
Desc column in my current row to that of the value in the previous row.
If the value of Code is different to the value in the previous row then
I want to output the row to a dataset. I managed to achieve this
functionality but in a very messy way I'm sure there must be a more
elegant solution to what I have come up with.

Can anyone come up with a simple algorithm to solve this?

Input dataset

ItemNum Code Desc
1 A Apple
2 A Cider
3 A Scrumpy
4 B Strawberry

Resulting dataset
Code Desc
A Apple Cider Scrumpy
B Strawberry
Joe

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Nov 17 '05 #2
just wanna make some correction for my post

"Ivan Wong" wrote:
My example assumes that you are the data store in a table called "Table1"

string prevCode;
DataTable table = ds.Tables["Table1"];
DataRow row = table.Rows[0];
prevCode = (string)row["Code"];
for(int i = 1; i < table.Rows.Count; i++)
{
row = table.Rows[i];
if(row["Code"] == prevCode)
{
(table.Rows[i-1])["Desc"] = String.Concat((table.Rows[i-1])["Desc"],
row["Desc"]);
table.Rows.RemoveAt(i);
i -= 1;
} else
{
prevCode = row["Code"];
} }

hope it helps,
Ivan Wong

"Joe" wrote:
I have a dataset as below. I want to iterate through the dataset
comparing the value of the Code column to the value in the previous
row. If they are the same then I want to concatenate the value of the
Desc column in my current row to that of the value in the previous row.
If the value of Code is different to the value in the previous row then
I want to output the row to a dataset. I managed to achieve this
functionality but in a very messy way I'm sure there must be a more
elegant solution to what I have come up with.

Can anyone come up with a simple algorithm to solve this?

Input dataset

ItemNum Code Desc
1 A Apple
2 A Cider
3 A Scrumpy
4 B Strawberry

Resulting dataset
Code Desc
A Apple Cider Scrumpy
B Strawberry
Joe

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Nov 17 '05 #3


"Ivan Wong" wrote:
My example assumes that you are the data store in a table called "Table1"

string prevCode;
DataTable table = ds.Tables["Table1"];
DataRow row = table.Rows[0];
prevCode = (string)row["Code"];
for(int i = 1; i < table.Rows.Count; i++)
{
row = table.Rows[i];
if((string)row["Code"] == prevCode)
{
(table.Rows[i-1])["Desc"] = String.Concat((table.Rows[i-1])["Desc"],
row["Desc"]);
table.Rows.RemoveAt(i);
i -= 1;
} else
{
prevCode = (string) row["Code"];
} }

hope it helps,
Ivan Wong

"Joe" wrote:
I have a dataset as below. I want to iterate through the dataset
comparing the value of the Code column to the value in the previous
row. If they are the same then I want to concatenate the value of the
Desc column in my current row to that of the value in the previous row.
If the value of Code is different to the value in the previous row then
I want to output the row to a dataset. I managed to achieve this
functionality but in a very messy way I'm sure there must be a more
elegant solution to what I have come up with.

Can anyone come up with a simple algorithm to solve this?

Input dataset

ItemNum Code Desc
1 A Apple
2 A Cider
3 A Scrumpy
4 B Strawberry

Resulting dataset
Code Desc
A Apple Cider Scrumpy
B Strawberry
Joe

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Nov 17 '05 #4
Joe
Thanks Ivan - this works very well and is three times less code than I
had previously! Thank you

Joe

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3222
by: Soul | last post by:
Hi, I am learning C# at the moment. I am trying to develop a simple program that will get data from a MS Access database into a dataSet. The result of dataSet should be something like: Year ...
1
2054
by: Jason Shohet | last post by:
What we've done so far in our web applications: 1. We have no datasets dragged-and-dropped in Visual Studio. They are all defined in the code, generated at runtime. 2. when a user updates or...
19
2507
by: Jaime Stuardo | last post by:
Hi all.. I have created a business logic component that is used from my ASP.NET webform. It works, but connection string to the database is hard coded, as in this method : public DataSet...
5
2347
by: codefragment | last post by:
Hi I have a dataset which holds (amongest other things) a string. - The dataset gets loaded from the database via a middle tier - The string value contains "\r\n" - Its serialised to the web...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.