Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
I've created a Dictionary like
 Dictionary<department,bool> dict= new Dictionary<department,bool>();
here Department is a class and I have `DepartmentId,Name and Code` for the departments. And in the `bool` am sending whether the person is `HOD` or not.
Am adding the records to this Dictionary like
here the records are successfully added to the Dictionary and after this am binding the Dictionary to a GridView like
now the inserted records are displayed fine in the gridview. After this am storing this records in the 'StaffDepartments' table in my database. I have 3 columns in the 'Staffdepartments' table
1.StaffId(PK - has link with the Staff table)
2.DepartmentId(PK - has link with the Department table)

here the records are stored fine in the database.No problem in adding the records into the database.
I have some questions here
****1.How can check whether the DepartmentId is already there in the Dictionary before adding to it.
2.When am editing the staff detail how can I delete the Selected Department from the Dictionary by checking the checkbox in Gridview rows.(here the records are coming from the database, so when I click delete button the records should be deleted in the database as well)****
if its a List instead of Dictionary, I can get the DepartmentId by
int departmentId = (int)gridDept.DataKeys[row.RowIndex].Values["DepartmentId"];
but in Dictionary i dunno how to do the same with Key and Value pairs....can anyone help me here.
Posted 17-Apr-12 2:28am
Edited 17-Apr-12 2:34am
abdul samathu at 17-Apr-12 7:49am
thanks Uday
Mohamed Mitwalli at 17-Apr-12 9:08am
Hi ,
1.How can check whether the DepartmentId is already there in the Dictionary before adding to it.
for This one i believe you have loop through Dictionary and compare the key and value with the one your trying to add .
2. For second one , It has to be some unique value to compare it with and if it true you removed from Dictionary and Database .
abdul samathu at 17-Apr-12 10:49am
thanks Mohamed Mitwalli, I dunno how to do this

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What you will need to do is to query your dictionary using LINQ
List<dict> mylist = new List<dict>
mylist = dict.where(x=>x.value.deptID=yourdepID).tolist();
// value exists
// to update you will need to create a new entry
// delete the old one
// insert the new value
//insert new
The syntax may not be 100% correct as I cannot test at the moment.
Hope this helps you.
abdul samathu at 18-Apr-12 0:36am
if I tried to create a List with dict, as u suggested reports error like "dict is a field, but is used like a field"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 Sergey Alexandrovich Kryukov 370
2 Maciej Los 285
3 ProgramFOX 275
4 CHill60 260
0 OriginalGriff 360
1 ProgramFOX 265
2 Maciej Los 235
3 CHill60 200
4 RyanDev 170

Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 17 Apr 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100