1
List<DataRow> rowList;

var rowDict = rowList.ToDictionary(dr => 
       double.Parse(dr.ItemArray[1].ToString()*5 
       - 10*double.Parse(dr.ItemArray[2].ToString), //Some calculation which
                                                    //sometime results in same key. 
       dr => dr
);

I want to ignore the key and data row insertion in the dictionary if the key is already inserted. Looking for doing it all in one iteration without calculating the key in advance and then carrying out the insertion. Looking to implement via Linq, have implemented using the Dict[key].

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • @Gseg, I have already gone through the answers suggested, unfortunately they didn't solved my purpose. I am specifically looking to check for key existence inside the ToDictionary(), where I am calculating the value of keys. Solutions I went through were eliminating duplicate keys before. Please unmark this question as duplicate. – Chandan K Singh Apr 27 '16 at 11:39
  • @hvd, : Please unmark it as duplicate. The question is specific to putting CheckKey in the parameter to ToDictionary where keys are being calculated. Neither the linked question highlights it nor the answers solve it. I have gone through the linked question several time before writing my own. – Chandan K Singh Apr 27 '16 at 12:27
  • You are requesting `to ignore the key and data row insertion in the dictionary if the key is already inserted`. These solutions do exactly that. In which way what you want to do will be different, and why is the difference important? – GSerg Apr 27 '16 at 14:52
  • 1
    @GSerg : I am calculating the key inside the the ToDictionary(), I don't know it in advance where I can call distinct and get the unique values. If I can access the Dictionary which is being populated inside ToDictionary(), I can simply put CheckKey() and it will not throw. For now, I am running foreach and doing the CheckKey before every Add(). Purpose is already solved, was just looking for one liner sleek code, nothing else. Thanks ! – Chandan K Singh Apr 28 '16 at 03:49

0 Answers0