Friday 15 April 2011

c# - Removing an item in Dictionary using the Value of the item [ not by key ] -



c# - Removing an item in Dictionary using the Value of the item [ not by key ] -

i having dictionary[int,ibooks], ibooks interface having bookname , author properties.

i have tired remove dictionary item value using keyvaluepair.

this code :

public override void remove() { dictionary<int, ibooks > books = new dictionary<int, ibooks>(); console.writeline("enter value removed : "); string value = console.readline(); //var item=books.first<kvp.value == value >; foreach (var item in books.where(kvp => kvp.value == value).todictionary()) //error line { books.remove(item); } }

ibook interface:

interface ibooks { string bookname { get; set; } string author { get; set; } }

but got error :

"operator == cant applied types of dictionary.ibooks , string"

can have thought solve or else there other improve way find element value ? ?

for illustration ,

my input bookname= "dbms" should remove particualr book dictionary.

but unable compare particular value "bookname". since interface has both author , bookname properties.

this statement not correct:

var item in books.where(kvp => kvp.value == value)

you comparing ibooks string that's causing build error.

note value of type string:

string value = console.readline()

compare string property bookname of ibook.

you have this:

var item in books.where(kvp => kvp.value.bookname == value)

c# generics dictionary collections

No comments:

Post a Comment