从方法返回分组数据
本文关键字:数据 返回 方法 | 更新日期: 2023-09-27 18:02:57
我有一个方法,我需要从中返回一个组,即:
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
}
但是我得到一个错误:
编辑:不能隐式转换类型"来。IGrouping"ConsoleApplication1.MyData"。存在显式转换(是吗?)
public class MyData
{
public string TaxDet{ get; set; }
public string empDetails { get; set; }
}
由于您正在组合GroupBy
和ToDictionary
,通过执行groupData[pointer]
,您将从字典中获得值,这是igrouing。如果需要从分组中提取数据,则还需要一个索引getter groupData[pointer][taxDet]
可能.ToDictionary(g => g.Key);
在这种情况下是多余的