-2

i have an Dictionary of type int, myclass. in myclass i have following attributes: an string, int, and an string array.

Evrything works fine when i save the data in dictionary. But i want now to look in the dictionary, if there is an value, in myclass, at the position string array. And filter them. For example every object that Contains in the Array "German", should be filtered out. Is there an possibility to do that?

myclass:

  public class MYCLASS
    {
        public int ID
        {
            get;
            set;
        }

        public string Specialty
        {
            get;
            set;
        }

        public string[] Language
        {
            get;
            set;
        }

 public myclass(int id, string specialty, string[] language)
        {
            this.Language= language;
            this.ID= id;
            this.Specialty = specialty;
        }

}

My Dictionary

Dictionary<int, myclass> objdict= new Dictionary<int, myclass>();
saaami11
  • 109
  • 2
  • 2
  • 8
  • 6
    I don't get it...Why does everybody who requests help is unable to put a good question up here. So where is your code, what have you tried, clarify your question and try to rephrase your question again please. My god... – ckruczek Jun 11 '15 at 06:13
  • Ou i see now that some text cuted. im sry.. – saaami11 Jun 11 '15 at 06:14
  • Posting code will be nice, because from your explanations it's hard to follow. – Gnqz Jun 11 '15 at 06:19
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 11 '15 at 06:24

4 Answers4

2

Yes, search by the values of the dictionary, then filter out the array

var dict = new Dictionary<int, MYCLASS>();
var filteredPairs = from pair in dict
                    where !pair.Value.Language.Contains("German")
                    select pair;
Eric
  • 5,675
  • 16
  • 24
1

If I understand your question correctly, if you have a class:

public class Country
{
    public string CountryName { get; set; }
    public int CountryId { get; set; }
    public string[]  States { get; set; }
}

If you have a dictionary:

 Dictionary<int, Country> dict = new Dictionary<int, Country>()
 {
     {1,new Country(){ CountryId=1,CountryName="Test1", States=new [] {"State1","State2","State3" }}},
     {2,new Country(){ CountryId=1,CountryName="Test2", States=new []{"State11","State21","State31" }}},
     {3,new Country(){ CountryId=1,CountryName="Test3", States=new []{"State12","State2","State31" }}},
     {4,new Country(){ CountryId=1,CountryName="Test4", States=new []{"Stae112","State21","State31" }}},
  };

Then you can filter it using Where:

 IEnumerable<KeyValuePair<int,Country>> enumerable=  dict.Where(x=>x.Value.States.Contains("State21",StringComparer.OrdinalIgnoreCase));
ANewGuyInTown
  • 5,957
  • 5
  • 33
  • 45
  • Thanks for ur solution. i have tried it and in enumerable i dont have any items.. – saaami11 Jun 11 '15 at 06:35
  • try case insensitive compare : `IEnumerable> enumerable= dict.Where(x=>x.Value.States.Contains("State21",StringComparer.OrdinalIgnoreCase));` – ANewGuyInTown Jun 11 '15 at 06:45
  • Okay i have tried this too, but still no items.. It worked with the foreach example of voytek. But thanks for ur help! – saaami11 Jun 11 '15 at 07:05
  • the reason you don't have data ( which actually you do )_ is because `IEnumerable` is subject to `deferred exectution`. That means the `expression` is evaluated until it's _realized_ value is actually required. This is extensively used in `Linq` for better performance. so, when you carried out the `foreach` after getting the `IEnumerable`, you are now actually executing the expressing and getting the values. – ANewGuyInTown Jun 12 '15 at 00:49
1

Using LINQ is the best option here. If you want access your class in regular loop, it will be like this:

        foreach (KeyValuePair<string, MYCLASS> entry in MyDic)
        {
            // Value is in: entry.Value and key in: entry.Key
            foreach(string language in ((MYCLASS)entry.Value).Language)
            {
                //Do sth with next language...
            }
        }
voytek
  • 2,202
  • 3
  • 28
  • 44
0

You can use a LINQ expression to find the records matching your query:

var dictionary = new Dictionary<int, MYCLASS>();
var results = dictionary.Where(record => !record.Value.Language.Any("German"));
sanderarts
  • 324
  • 2
  • 10