0

i am not sure it is possible or not.

i want to build a dictionary with Multi column key(let's say 4). then search by a few columns(2 or 3).

code:

class keyset
{
     public keyset()
     {}

     public long sId;
     public long aId;
     public string Name;
     public string title;

}


Dictionary<keyset, string> myDictionary = new Dictionary<keyset, string>();

Then i want to search by only a few column in the keyset:

case 1:    sid = 100  and  aid=200
case 2:    sid = 50  and name="John"
.......

how can i do this?


Ok i understand that is not possible. I should use linq.

Then i want to know which structure will be faster:

plan A: use dictionary

class keyset
{
     public keyset()
     {}

     public long sId;
     public long aId;
     public string Name;
     public string title;

}

Dictionary<keyset, string> myDictionary = new Dictionary<keyset, string>();

plan B: without dictionary

class data
{
     public data()
     {}

     public long sId;
     public long aId;
     public string Name;
     public string title;


     public string  Value;      <-------- 

}
Benny Ae
  • 1,897
  • 7
  • 25
  • 37

2 Answers2

0

A Dictionary<TKey,TValue> is not a database, so, no, you can't do that.

On the other hand, a DataSet is meant to be an in-memory representation of something like a database. If you use DataSet instead of Dictionary<TKey,TValue>, then you'll be able to use the Find method to find the data you want.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

If you have a common logic for comparing keysets you can implement IEquatable<T> in your class. But if you want to compare them based on different properties each time this is not an option.In that case you can try using LINQ:

var itemExists = myDictionary.Any(x => x.Key.sid == 50 && x.Key.name == "John");

if(itemExists)
{
   var value = myDictionary.First(x => x.Key.sid == 50 && x.Key.name == "John").Value;
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • so is this the same speed if i just use linq then put "values" into same row without using a dictionary? – Benny Ae Dec 11 '14 at 22:18
  • It's too soon to worry about performance. Worry about performance after the code works. "If it doesn't work, it doesn't matter how fast it doesn't work" — Mich Ravera. See http://stackoverflow.com/a/1903245/76337 – John Saunders Dec 11 '14 at 22:29