473,624 Members | 2,223 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 1114
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.Coun t; 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.Remo veAt(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.Coun t; 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.Remo veAt(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.Coun t; 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.Remo veAt(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
3232
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 Semester Code Task --------------------------------------------------- "2003" "One" "CSE9020" "Deliverable Item 1" "2003" "One" "CSE9020" "Deliverable Item 2"
1
2062
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 hits a 'delete' button next to a row in a datagrid, we call a webservice. In there, we coded insert, update & delete functionality. I'm tired of seeing all that update logic in our code. I am looking for a way to have .NET generate its own...
19
2520
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 GetCategories() { SqlConnection conn = new SqlConnection("Data Source=DEVSERVER;Initial Catalog=XXXX;User ID=X;Password=Y");
5
2355
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 application - The web application deletes the row. I can see using the below that before and after the delete the string value is what it should be (string)rows;
0
8174
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8624
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1485
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.