Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 为什么在使用GROUP BY后,列表中所有对象的一个字段都设置为零?_C#_Linq_List_Lambda - Fatal编程技术网

C# 为什么在使用GROUP BY后,列表中所有对象的一个字段都设置为零?

C# 为什么在使用GROUP BY后,列表中所有对象的一个字段都设置为零?,c#,linq,list,lambda,C#,Linq,List,Lambda,在下面的代码中,val.supports中的每个对象都有名称、SupportMount和GFP,因此在出现之前一切正常。当支持者对象的positivesupporters列表位于类播放器的字段上时,列表中所有支持者的GFP字段为0。我为什么要解决这个问题?我应该怎么做 for (int i = 0; i < result["modelData"]["rows"].Count; i++) { row = result["modelData"]["rows"][i];

在下面的代码中,val.supports中的每个对象都有名称、SupportMount和GFP,因此在出现之前一切正常。当支持者对象的positivesupporters列表位于类播放器的字段上时,列表中所有支持者的GFP字段为0。我为什么要解决这个问题?我应该怎么做

   for (int i = 0; i < result["modelData"]["rows"].Count; i++)
   {
    row = result["modelData"]["rows"][i];

    if (row["f"][0]["v"] != null && row["f"][1]["v"] != null)
  {
  if (dictionary.ContainsKey((string)row["f"][0]["v"]))
  val.supporters.Add(new Supporter { name = (string)row["f"][0]["v"], supportAmount = (double)row["f"][1]["v"], GFP = dictionary[(string)row["f"][0]["v"]] });
   }
   }


  val.positiveSupporters = val.supporters.GroupBy(x => x.name).Select(x => new Supporter { name = x.Key, supportAmount = x.Sum(y => y.supportAmount)}).ToList();

正如评论中提到的,我没有选择GFP,它被设置为默认值0。鉴于我的问题中每个名字都有一个固定的GFP,我使用firsr选择GFP:

val.positiveSupporters = val.supporters.GroupBy(x => x.name).Select(x => new Supporter { name = x.Key, supportAmount = x.Sum(y => y.supportAmount),GFP = x.First().GFP}).ToList();

或者更好的是,在流程的第二部分获得GFP。它似乎是基于分组键的查找

var positiveSupporters = supporters.GroupBy(x => x.name).Select(x => new Supporter { name = x.Key, supportAmount = x.Sum(y => y.supportAmount), GFP = dictionary[x.Key] }).ToList();

我假设您也需要在lambda声明中选择GFP。由于未选中,所有值都默认为int的默认值,即0。左侧的空白部分用于记笔记?如果看不到您对什么是字典以及该位置包含什么的定义,我无法告诉您如何修复它。什么类型的物体是GFP?GFP是双重的。这就是字典的样子:字典=新字典;dictionary.AddUSA,1;手术死亡我知道,但我该怎么做?是的,这更有意义!!谢谢