Solved

Remove row from data table (hash table) C#  vs2010

Posted on 2013-12-24
7
653 Views
Last Modified: 2014-02-23
Background:  I am working on an asp.net website (intranet) using c# that employs proprietary web methods to download students exam material from a hosted web site.  
I have no control over what gets downloaded from a hosted site.  I do have control over what is displayed.   I am displaying an exam details page that opens from a link on a course page.   The problem is the hosted site is not filtering out previous exam results (for same exam) which is bad for me.   I am comparing dates and attempting to delete the older rows.  My aim is to display only one exam.  
I need help with the syntax to delete a row from a dataset, or I can select a row to publish to the databound fields on the html page.  I feel the later might be harder than just deleting the row.

ds is the dataset downloaded from hosted website, and configured with xml stylesheet.
Table:  Lesson
Column which compare is made:  LessonStatusDate

Code:
 if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row
                lessonCount = 0;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                    DateTime dt1 = DateTime.Now;

                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount > 1) && (dt.Date > dt1.Date))
                    {
                        //Delete dt1.Date
                       dt1=dt.Date
                    }
                    lessonCount++;
                }
            }


Thanks in advance for your help.   ...and yes, some of us used up all our vacation days and are working on Christmas Eve.  ;-(
0
Comment
Question by:JonDavid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
7 Comments
 

Author Comment

by:JonDavid
ID: 39738227
I have made progress, but am now deleting the wrong date:
if (lesson.Rows.Count > 1)
            {anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                //Keep the most recent row
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to
 foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt.Date > dt1.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                        dt1 = dt.Date;
                    }

                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
          }
0
 

Author Comment

by:JonDavid
ID: 39738260
This is my third time through, and it works.   Please comment if there is a better way to do this:
if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt1.Date > dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                        dt1 = dt.Date;
                    }
                    else if((lessonCount >=1) && (dt1.Date < dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount-1].Delete();
                    }

                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
            }
0
 

Author Comment

by:JonDavid
ID: 39738298
Found on potential error.  Should I ever see three rows, I needed to set the dt1 to the max value.   My original request was for help with the syntax.  This code works, but I think it can be done much better.  Would appreciate any help I can get on this matter.

 if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row, delete all others.
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt1.Date > dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                    }
                    else if((lessonCount >=1) && (dt1.Date < dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount-1].Delete();
                        dt1 = dt.Date;
                    }
                   
                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
            }
                       }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 39738880
Hi Jon,
It can be easily done using Linq where we can apply group by and order by  as sql server.
please find following code for this :
DataTable lession = new DataTable("lession");
            lession.Columns.Add(new DataColumn("LessionId", typeof(string)));
            lession.Columns.Add(new DataColumn("LessonStatusDate", typeof(DateTime)));
            lession.Columns.Add(new DataColumn("LessonGrade", typeof(string)));
           
            //add records for lession 1
            lession.Rows.Add(new object[] { 1, "12/12/13","A" });
            lession.Rows.Add(new object[] { 1, "12/10/13","B" });
            lession.Rows.Add(new object[] { 1, "12/22/13","A" });
            //add records for lession 2
            lession.Rows.Add(new object[] { 2, "12/15/13","B" });
            lession.Rows.Add(new object[] { 2, "11/22/13","A" });
            //add records for lession 3
            lession.Rows.Add(new object[] { 3, "12/18/13","O" });
            lession.Rows.Add(new object[] { 2, "12/05/13" ,"A"});
           
            //add datatable to dataset
            ds.Tables.Add(lession);


            // group by lession id and order by LessonStatusDate descending and add first record of each group to result table
            DataTable dt = ds.Tables["lession"].AsEnumerable()
                .GroupBy(r => new { Col1 = r["LessionId"] }) 
                .Select(g => g.OrderByDescending(r => r["LessonStatusDate"]).First()).CopyToDataTable();  

Open in new window


now, in datatatble dt will be having filtered recordset for each lession.hope this helps. Merry Christmas !!!
0
 

Accepted Solution

by:
JonDavid earned 0 total points
ID: 39742024
I can see where this would be helpful.   Since all of the data is downloaded through XML style sheets, and a string reader, I would to make a few changes, but overall, this would give more lattitude in manipulated style, and content to the page.   Thanks!  Hope you had a Merry Christmas.
0
 

Author Comment

by:JonDavid
ID: 39868037
Project is closed and passed UAT.  Will go into production next week.
0
 

Author Closing Comment

by:JonDavid
ID: 39880360
The answer given by the previous person required more time than was alotted in project.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question