Question

I have a method from which I need to return a group, i.e.:

public static MyData BinSearch(MyData searchDate)
{
 // First doing a binary search to get the index

 if (index >= 0)
        {
            return recordList[index];
        }

        index = ~index;

        if (index == 0 || index == recordList.Count)
        {
            return null;
        }

        int newIndex = (((index-1)+index)/2)+1;
        string pointer = recordList[newIndex].TaxDet;

        var groupData = recordList.GroupBy(p => p.TaxDet)
                      .ToDictionary(g => g.Key);

        var output = groupData[pointer];


        return (output); // Here I want to return a group of data
 }

But I'm getting an error:

Cannot implicitly convert type 'System.Linq.IGrouping' to 'ConsoleApplication1.MyData'. An explicit conversion exists (are you missing a cast?)

EDIT:

public class MyData
    {
        public string TaxDet{ get; set; }
        public string empDetails { get; set; }
    }
Était-ce utile?

La solution

Since you are combining GroupBy and ToDictionary, by doing groupData[pointer] you are getting value from dictionary, which is IGrouping. If you need to extract data from grouping, you need 1 more index getter groupData[pointer][taxDet]

Probably .ToDictionary(g => g.Key); is redundant in this case

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top