Home > OS >  Delete a record by matching Value
Delete a record by matching Value

Time:12-23

I have a dictionary where values are stored in the following format -

userID, empDetails

For example,

1234, 'empName,jobDesc,CardNumber,Type'

I have to compare this information with another set of information such that -

  • If entered userId is present in the above dictionary, then remove this record from the dictionary.
  • If entered CardNumber is present (here userId is not known) in the above dictionary, then remove this record from the dictionary.

The first condition is simple and can be done by

dictionary.Remove(key)

But I am confused as how would I implement the second condition. I want something like

if(CardNumber.PresentinAboveDictionary)
then
Remove that record

I know we can compare a partial string in key like this, but I want to remove the record. Check if any part of a hashtable value contains certain string c#

CodePudding user response:

Assuming the employment details in your dictionary are a string in the specified format you would need to:

  1. Search the values within the dictionary
  2. Parse/Split the values to get the card numbers
  3. Check the card numbers to see if they match the card number you are checking
  4. Return the key value pair when a match occurs
  5. Remove the entry for the key in the returned key value pair

Example code for the solution:

var dictionary = new Dictionary<int, string>() { { 1, "empName,jobDesc,124124134,Type" } };
                
var cardNumber = 124124134;
var entry = dictionary.FirstOrDefault(x => DoEmploymentDetailsContainCardNumber(x.Value, cardNumber));

if (!entry.Equals(default(KeyValuePair<int, string>)))
{
    dictionary.Remove(entry.Key);
}

Method that checks if card number is present in employment details:

private static bool DoEmploymentDetailsContainCardNumber(string empDetails, int cardNumber)
{
    var splitEmpDetails = empDetails.Split(',');
    var empDetailsCardNumber = splitEmpDetails[2];
    return empDetailsCardNumber == cardNumber.ToString();
}

CodePudding user response:

Instead of Dictionary you can use a strongly typed List

  1. Use the Linq builtin Remove method
  2. Use Parallel.ForEach, iterate the list and remove the item (beware, takes more time)

pseudo code:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Collections;

namespace ConsoleApp4
{
    public class Employee
    {
        public Employee(int userID, string empDetails)
        {
            string[] props = empDetails.Split(new char[] { ',' }, StringSplitOptions.None);
            this.userID = userID;
            this.empName = props[0];
            this.jobDesc = props[1];
            this.CardNumber = props[2];
            this.Type = props[3];
        }

        public int userID { get; set; }
        public string empName { get; set; }
        public string jobDesc { get; set; }
        public string CardNumber { get; set; }
        public string Type { get; set; }
    }

    public class MyCustomList : List<Employee>
    {
        public void Add(int userID, string empDetails)
        {
            this.Add(new Employee(userID, empDetails));
        }

        public bool Remove(string CardNumber)
        {
            bool found = false ;
            Parallel.ForEach(this,
            (i, state) =>
            {
                if (i.CardNumber == CardNumber)
                {
                    this.Remove(i);
                    state.Break();
                }
            });
            return found;
        }

        public bool RemoveV2(string CardNumber)
        {
            bool found = false;
            if (this.Any(x => x.CardNumber == CardNumber))
            {
                this.Remove(this.Where(x => x.CardNumber == CardNumber).First());
                found = true;
            }
            return found;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var dict = new MyCustomList();//userID, empDetails list
            dict.Add(12341, "empName1,jobDesc,CardNumber1,Type");
            dict.Add(12342, "empName2,jobDesc,CardNumber2,Type");
            dict.Add(12343, "empName3,jobDesc,CardNumber3,Type");
            dict.Add(12344, "empName4,jobDesc,CardNumber4,Type");
            dict.Add(12345, "empName5,jobDesc,CardNumber5,Type");
            dict.Add(12346, "empName6,jobDesc,CardNumber6,Type");
            dict.Add(12347, "empName7,jobDesc,CardNumber7,Type");
            dict.Add(12348, "empName8,jobDesc,CardNumber8,Type");

            //remove CardNumber5
            dict.Remove("CardNumber5");
            Console.Write(dict);
        }
    }
}

CodePudding user response:

you can follow the simple approach to remove the key by using a loop here.

Here I am assuming that there is no key with a value of -1 in the dictionary.

int keyToRemove = -1;
foreach (var entry in dictionary)
{
    if (entry.Value.Contains(CardNumber))
    {
        keyToRemove = entry.Key;
        break;
    }
}

if (keyToRemove != -1)
{
    dictionary.Remove(keyToRemove);
}

CodePudding user response:

before you remove the record you could make validation like this :

`foreach (key in PresentinAboveDictionary.Keys)
{

//if you're using List<T> as second argument, you could point the CardNumber.PresentinAboveDictionary using index (which is 2 in your case)
if (PresentinAboveDictionary[key][2] == CardNumber) 
{
    PresentinAboveDictionary.Remove(key)
}

}

CodePudding user response:

you can do something like this.

var recordsToRemove = dictionary.Where(x => x.Value.Contains("what you are looking for"))
                                .ToList();

        if (recordsToRemove.Any())
        {
            foreach (var record in recordsToRemove)
            {
                dictionary.Remove(record.Key);
            }
        }
  • Related