Share via


Compare List to Dictionary

Question

Tuesday, July 2, 2013 11:43 AM

I need a little help here guys. I'm trying to figure out how to compare the values of a List and a dictionary. If the values don't exist in the dictionary, I'd like to remove them from the List. Anyone have any ideas how to do this? Here is what I have for code so far:

 

        string Students = Session["Checkvals"].ToString();      
        char[] arr = new char[] { ' ' };
        Students = Students.TrimStart(arr);

        string[] Student = Students.Split(' ');
        List<string> list = new List<string>();
        string StudentList = string.Empty;
        foreach (string word in Student)
        {
            if (!list.Contains(word))
            {

All replies (7)

Tuesday, July 2, 2013 12:23 PM âś…Answered

If you only want to remove values from your List that are not contained within your Dictionary, you could use the following : 

//Check to ensure that your Session key exists
if(Session["Checkvals"] != null)
{
    //If so then clean up your Session value
    string students = Session["Checkvals"].ToString().TrimStart(' ');

    //Create your Array of students (using the Split() method)
    List<string> studentList = students.Split(' ').ToList();

    //Now you want to remove all of the Students that are not contained within your Dictionary
    studentList = studentList.Where(s => YourDictionary.Keys.Any(k => k == s)).ToList();
    
}

The above should only grab the Students within your collection if there exists a Key within the Dictionary that matches the Student Name. If you wanted to check for values as well, you could slightly modify the above statement and use the Dictionary.ContainsKey() and Dictionary.ContainsValue() methods :

//Using Keys
studentList = studentList.Where(s => YourDictionary.ContainsKey(s)).ToList();

//Using Values
studentList = studentList.Where(s => YourDictionary.ContainsValue(s)).ToList();

Tuesday, July 2, 2013 12:02 PM

@ bbcompent1 

you're mostly on the right track ... i'm assuming the words in your List<String> are the keys to your Dictionary<String, ?>

in your code snippet, you're asking whether the List contains the Key ...

Daniel, you need to query the Dictionary ... http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

 

the methods are http://msdn.microsoft.com/en-us/library/kw5aaea4.aspx ContainsKey or for values http://msdn.microsoft.com/en-us/library/a63811ah.aspx for ContainsValue.

 

g.


Tuesday, July 2, 2013 12:15 PM

In that code, what I'm doing is using the viewstate to track the checkboxes that have been selected. I use that code to clear out any duplicate IDs that exist. Yes, the Dictionary values are string and so are the values from the List. I'll check it out and get right back to you, thanks Gerry!


Tuesday, July 2, 2013 12:57 PM

Ok, now how do I remove duplicates from my studentList List?


Tuesday, July 2, 2013 1:13 PM

If your list is a just a List of strings, you can use the Distinct() method to handle this : 

studentList = studentList.Distinct();

Tuesday, July 2, 2013 2:17 PM

A little more information on this dictionary, I have an ID which is the username, the key is a boolean true or false. If the ID has a boolean that is true, it should be in my listing, if false, it should not be. I realized that the listing that I'm trying to output to screen is actually a string value. Maybe that is part of my problem?

 

protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        string myvar = string.Empty;
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox box = row.FindControl("ChkSelect") as CheckBox;
            if (box != null && box.Checked)
            {
                HiddenField field = row.FindControl("HiddenField1") as HiddenField;
                myvar = myvar + " " + field.Value.ToString();
            }
        }
        Session["MyCheckBoxes"] = Session["MyCheckBoxes"] + myvar.ToString();

        string Students = Session["MyCheckBoxes"].ToString();

        char[] arr = new char[] { ' ' };
        Students = Students.TrimStart(arr);

        string[] Student = Students.Split(' ');
        List<string> list = new List<string>();
        string StudentList = string.Empty;
        foreach (string word in Student)
        {
            if (!list.Contains(word))
            {
                UserInformation _u2 = new UserInformation(word, false);
                if (_u2.Name != null)
                {
                    StudentList = "<br />" + _u2.Name + " - " + _u2.UserPin + "";
                    _u2 = null;
                }
                list.Add(word);
            }
        }
        lblList.Text = StudentList.ToString();

    }

Tuesday, July 2, 2013 2:37 PM

Related posting, seems to be different problem.

http://forums.asp.net/t/1918822.aspx/1?Checkboxes+in+my+gridview+being+unchecked+don+t+remove+value+from+Dictionary