Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2008, 07:18 PM
Authorized User
 
Join Date: Nov 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default removing duplicates

Hi in my example i hope i'm not being overly complicated for the simple task at hand. I'm calling much data from a table and there are multiple duplicate label names. I need every record being pulled, but in this case i'm hoping to display only the unique names from the data. So imagine a thousand records with label being from 1 to 10, there are many duplicate label names. This is what i've tried to do to get only the unique in a DataList to display them in the aspx page (am i being overly complicated..?) I'm having issues getting it working, any thoughts?

Code:
        static List<string> removeDuplicates(List<string> inputList)
        {
            List<string> finalList = new List<string>();
            foreach (string currValue in inputList)
            {
                if (!Contains(finalList, currValue))
                {
                    finalList.Add(currValue);
                }
            }
            return finalList;
        }

        static bool Contains(List<string> list, string comparedValue)
        {
            foreach (string listValue in list)
            {
                if (listValue == comparedValue)
                {
                    return true;
                }
            }
            return false;
        }

        protected void dlGoalsDataBound(object sender, DataListItemEventArgs e)
        {
            ArrayList Goals = new ArrayList();
            Goals.Add(e);

            List<string> result = removeDuplicates(Goals[0]);

            result.ForEach(
                delegate(string s)
                {
                HyperLink tmpLnk = (HyperLink)e.Item.FindControl("HyperLink1");
                Label tmpLbl = (Label)e.Item.FindControl("Label1");
                Session["GoalTypeID"] = tmpLbl.Text;
                tmpLnk.NavigateUrl = "~/area/default.aspx?nav=g&id=" + tmpLbl.Text + "";
                }
                ); 

        }


 
Old February 25th, 2008, 08:09 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Why aren't you just doing a SELECT DISTINCT from your database?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old February 25th, 2008, 08:19 PM
Authorized User
 
Join Date: Nov 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

cause i'm not using the db query exclusive for this, thanks

 
Old February 26th, 2008, 06:10 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Code:
ArrayList Goals = new ArrayList();
Goals.Add(e);

List<string> result = removeDuplicates(Goals[0]);
The above section of code is nonsense. You create an array, add 'e' to it and then pass the first element of the array (which will be 'e') to the function. This is therefore equivalent to the following:

Code:
List<string> result = removeDuplicates(e);
However - I suspect that 'e' is not List<string> so I am suprised this code even compiles, let alone runs.

Also, List<string> already contains a Contains method.

Code:
static List<string> removeDuplicates(List<string> inputList)
{
  List<string> finalList = new List<string>();

  foreach(string s in inputList)
  {
    if( !finalList.Contains(s) ) finalList.Add(s);
  }
  return finalList;
}
However, this does not get around the issue of where you get the first list from.

/- Sam Judson : Wrox Technical Editor -/
 
Old February 26th, 2008, 09:22 AM
Authorized User
 
Join Date: Nov 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

samjudson

[2]the added contain actually speeds up the processing.

[1]Thanks for the first part, your correct

 
Old February 26th, 2008, 09:44 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Another alternative which should definitely be quicker would be to Sort the list and then use the RemoveAll with a predicate to remove the duplicates:

Code:
result.Sort();
result.RemoveAll(RemoveDuplicates);

private static string lastString = String.Empty;

private static bool RemoveDuplicates(string a)
{
  if( a == lastString )
  {
    return true;
  }
  lastString = a;
  return false;
}
/- Sam Judson : Wrox Technical Editor -/
 
Old February 26th, 2008, 11:01 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Havey,

Why don't you use a SortedDictionary with a try block around the Add to catch duplicates. In the example I am assuming that DataListItemEventArgs is an array of DataListItemEventArg and that DataListItemEventArg is an object with a property named "Goal". Of course, only you know what DataListItemEventArgs is so you may have to adapt the example accordingly.

I.E.:
Code:
protected void dlGoalsDataBound(object sender, DataListItemEventArgs e)
{

  // Create a new sorted dictionary of DataListItemEventArg, with string
  // keys.
  SortedDictionary<string, DataListItemEventArg> Goals = 
    new SortedDictionary<string, DataListItemEventArg>();

  for(int i=0;i<e.Length;i++) 
  {
    // The Add method throws an exception if the new key is 
    // already in the dictionary.
    try
    {
      Goals.Add((DataListItemEventArg)e[i].Goal, e[i]);
    }
    catch (ArgumentException)
    {
      Console.WriteLine("The Goal {0} has already been added.", e[i].Goal);
    }
  }

  ... more code here ...
}
This would combine sorting the data as you input it and eliminating duplcates.

What you don't know can hurt you!
 
Old February 26th, 2008, 01:04 PM
Authorized User
 
Join Date: Nov 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks David, i tried that kinda method and had issues converting generic list to collection array.

Maybe i need to explain my self better...sry,
there is default.aspx page that uses a masterPage. In the default.aspx.cs there is:
Code:
List<HealthGoal> goals = null;

goals = SiteProvider.Member.GetGoalsForMember(Convert.ToInt32(Session["PersonID"]));

..... create an xml string of the values to pass to the default.aspx via Page.ClientScript...blah blah
.....
now the goals[i].GoalTypeID (numeric) is what i want the unique list of.

In the master page.aspx i have:
Code:
<asp:DataList ID="dlResults" runat="server" OnItemDataBound="dlResultsDataBound" Width="150">
                                                    <ItemTemplate>
                                                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                                            <tr>
                                                                <td width="1%" valign="top"><img alt="" src="/images/members-area/subnav-bullets/green.gif" width="28" height="20"></td>
                                                                <td valign="top">
                                                                    <asp:HyperLink CssClass="subnav" ID="HyperLink1" runat="server"><%# Container.DataItem %></asp:HyperLink>
                                                                    <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem %>' Visible="false"></asp:Label>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </ItemTemplate>
                                                </asp:DataList>
and the behind code is the where the code were are talking about should go. So now that you have the whole picture, any ideas how to DataBind the unique list of goals[i].GoalTypeID

I'm trying to incorporate your diction idea, as i think your thought has much validity, this is what i'm starting with:

Code:
protected void dlGoalsDataBound(object sender, DataListItemEventArgs e)
        {

            ArrayList DataListItemEventArg = new ArrayList();

            DataListItemEventArg = SiteProvider.Member.GetGoalsForMember(Convert.ToInt32(Session["PersonID"]));

            SortedDictionary<string, DataListItemEventArg> Goals = new SortedDictionary<string, DataListItemEventArg>();

            for (int i = 0; i < e.Length; i++)
            {
                try
                {
                    Goals.Add((DataListItemEventArg)e[i].Goal, e[i]);
                }
                catch
                {
                   //
                }
            }

            dlGoals.DataSource = Goals;
            dlGoals.DataBind();
        }




 
Old February 26th, 2008, 01:14 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

DataListItemEventArgs is just a wrapper for the event arguments fired by the data grid, I think you're getting it confused with something.

http://msdn2.microsoft.com/en-us/lib...eventargs.aspx

This should work:

Code:
List<HealthGoal> goals = SiteProvider.Member.GetGoalsForMember(Convert.ToInt32(Session["PersonID"]));

List<int> finalGoals = new List<int>();

foreach(HealthGoal g in goals)
{
  if( !finalGoals.Contains(g.GoalTypeId) )
  {
    finalGoals.Add(g.GoalTypeId);
  }
}

dlGoals.DataSource = finalGoals;
dlGoals.DataBind();
/- Sam Judson : Wrox Technical Editor -/
 
Old February 26th, 2008, 02:05 PM
Authorized User
 
Join Date: Nov 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, thank you very much






Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing duplicates hewstone999 Access VBA 2 November 4th, 2008 05:26 PM
Yes to duplicates MMJiggy62 Beginning VB 6 3 July 14th, 2006 11:51 AM
Avoiding Duplicates fixitman Access 1 April 27th, 2004 10:43 AM
Deleting duplicates prabodh_mishra SQL Server 2000 3 September 23rd, 2003 09:04 AM
Duplicates ashley_y Access 11 August 14th, 2003 03:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.