Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过分组方法在多个属性上使用linq分组_C#_Linq_Grouping - Fatal编程技术网

C# 通过分组方法在多个属性上使用linq分组

C# 通过分组方法在多个属性上使用linq分组,c#,linq,grouping,C#,Linq,Grouping,我有以下代码: var linqResults = (from rst in QBModel.ResultsTable group rst by GetGroupRepresentation(rst.CallerZipCode, rst.CallerState) into newGroup select newGroup ).ToList(); 使用分组方法: private string[] GetGroupReprese

我有以下代码:

var linqResults = (from rst in QBModel.ResultsTable
            group rst by GetGroupRepresentation(rst.CallerZipCode, rst.CallerState) into newGroup
            select newGroup
           ).ToList();
使用分组方法:

private string[] GetGroupRepresentation(string ZipCode, string State)
{
  string ZipResult;
  if (string.IsNullOrEmpty(ZipCode) || ZipCode.Trim().Length < 3)
    ZipResult = string.Empty;
  else
    ZipResult = ZipCode.Substring(0, 3);

  return new string[]{ ZipResult, State };
}
private string[]GetGroupRepresentation(字符串ZipCode,字符串状态)
{
弦乐;
if(string.IsNullOrEmpty(ZipCode)| | ZipCode.Trim().Length<3)
ZipResult=string.Empty;
其他的
ZipResult=ZipCode.Substring(0,3);
返回新字符串[]{ZipResult,State};
}
这运行得很好,但根本不分组。QBModel.ResultsTable有427条记录,linq运行后,linqResults仍有427条记录。在调试中,我可以看到相同的截短邮政编码和州名的双重备份。我猜这与我从分组方法返回的数组有关

我做错了什么

如果我在不使用数组的情况下连接被截断的邮政编码和州名称的返回值,我将得到84个分组

如果我去掉rst.CallerState参数并将分组方法更改为:

private string GetGroupRepresentation(string ZipCode)
{
    if (string.IsNullOrEmpty(ZipCode) || ZipCode.Trim().Length < 3)
      return string.Empty;
    return ZipCode.Substring(0, 3);
}
私有字符串GetGroupRepresentation(字符串ZipCode)
{
if(string.IsNullOrEmpty(ZipCode)| | ZipCode.Trim().Length<3)
返回字符串。空;
返回ZipCode.Substring(0,3);
}
它将返回我66组

我真的不想连接组值,因为我想稍后单独使用它们,这是错误的,因为它基于数组是否工作,但是,有点类似于以下内容:

List<DataSourceRecord> linqResults = (from rst in QBModel.ResultsTable
        group rst by GetGroupRepresentation(rst.CallerZipCode, rst.CallerState) into newGroup
        select new MapDataSourceRecord()
        {
          State = ToTitleCase(newGroup.Key[1]),
          ZipCode = newGroup.Key[0],
          Population = GetZipCode3Population(newGroup.Key[0])
        }).ToList();
List linqResults=(来自QBModel.ResultsTable中的rst
按GetGroupRepresentation(rst.CallerZipCode,rst.CallerState)将rst分组到新组中
选择新的MapDataSourceRecord()
{
State=ToTitleCase(newGroup.Key[1]),
ZipCode=newGroup.Key[0],
Population=GetZipCode3Population(newGroup.Key[0])
}).ToList();

数组是引用类型,因此当分组方法比较具有相同值的两个数组时,无法确定它们是否相同,因为引用不同。你可以读更多


一种解决方案是考虑使用一个类,而不是使用函数结果的数组,并使用另一个类来比较实现IEqualityComparer接口的结果,并将其传递给GroupBy方法,以便分组方法可以找到ZipCode和State的哪些组合是真正相等的

不确定这是否有效,因为我无法复制您的代码
但在进行分组之前,在单独的变量中添加组键和字符串[]可能会更容易。像这样

var linqdatacleanup = QBModel.ResultsTable.Select(x=> 
new { 
     value=x,  
     Representation = GetGroupRepresentation(rst.CallerZipCode, rst.CallerState),
     GroupKey= GetGroupRepresentationKey(rst.CallerZipCode, rst.CallerState)
}).ToList();
因此GetGroupRepresentationKey返回单个字符串,而GetGroupRepresentation返回字符串[]

这将允许您在此数据集上进行分组,并根据需要访问数据

但是,在您花太多时间讨论这个问题之前,请检查一下堆栈溢出问题。也许会有帮助