Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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

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# 按日期搜索对象的最大值列表_C#_Linq_Foreach - Fatal编程技术网

C# 按日期搜索对象的最大值列表

C# 按日期搜索对象的最大值列表,c#,linq,foreach,C#,Linq,Foreach,我希望我能很好地解释我的情况,以便得到一些帮助 基本上,我有一个由ItemRows组成的列表(ItemList),如下所示 ItemRows public string ItemId { get; set; } public string ItemDate { get; set; } public string ItemPrice { get; set; } 目前我在一个foreach循环中,它在每次迭代中将当前变量传递给另一个函数。然而,当我通

我希望我能很好地解释我的情况,以便得到一些帮助

基本上,我有一个由ItemRows组成的列表(ItemList),如下所示

ItemRows
        public string ItemId { get; set; }
        public string ItemDate { get; set; }
        public string ItemPrice { get; set; }
目前我在一个foreach循环中,它在每次迭代中将当前变量传递给另一个函数。然而,当我通过价格时,我只想通过每个ItemDate的最高价格

foreach item in ItemList
{
    AnotherFunction(ItemId, ItemDate, MaxPriceByDate);
}
因此,如果我有以下4行数据

123, 01/01/2015, $15
234, 01/01/2015, $20
345, 01/02/2015, $10
456, 01/02/2015, $5
以下是我希望循环传递信息的方式:

first iteration:  123, 01/01/2015, $20
second iteration: 234, 01/01/2015, $20
third iteration:  345, 01/02/2015, $10
fourth iteration: 456, 01/02/2015, $10
基本上,我在寻找如何从foreach循环正在使用的列表中选择美元金额的帮助,但我只希望它在每次迭代中从所述列表中按日期选择最高金额


我希望这是有意义的,我感谢你的帮助

您可能希望按
ItemDate
属性进行分组,并以稍微不同的方式进行处理。例如

 var grouped = (from r in rows
                 group r by r.ItemDate into g
                 select new { Date = g.Key, MaxPrice = g.Max(gg=>gg.ItemPrice)
                 Items = g})
这将为您提供一个结构,其中每个元素都有一个日期、该日期的MaxPrice以及属于该日期的项目。通过对循环进行一些小的修改,您可以将其适应当前的结构


编辑:如另一个答案中所述,如果价格是字符串形式或与日期属性相同,则可能必须将其转换为某种数字格式。我建议在进入这个逻辑之前,先把字符串转换成日期和数字

如果您只需要
MaxPricePerDate
,则可以使用以下linq函数:

foreach(var item in ItemList)
{
    AnotherFunction(item.ItemId, item.ItemDate, ItemList.Where(x => x.ItemDate == item.ItemDate)
                                                        .OrderByDesc(z => Convert.ToInt32(z.ItemPrice.Replace("$", "")))
                                                                                 .First().ItemPrice);
}
int nMaxPrice = ItemList.Where(i => i.ItemDate == item.ItemDate
                        .Max(i => Convert.ToInt32(i.ItemPrice));
此外,如果字符串确实包含
$
符号,则需要首先将其剥离,如
i.ItemPrice.Replace(“$”,”)


需要调用
Convert
将字符串转换为int,但是这很危险,因为如果字符串格式不正确,则可能引发异常。还考虑查看<代码> It32.TyPARSER();

您可以使用
Linq
查找当前数据的最高价格。这可能不是最有效的方法,但它会起作用:

  foreach(var item in itemList)
  {
        Console.WriteLine("{0} {1} {2}", item.ItemId, item.ItemDate, itemList.Where(i => i.ItemDate == item.ItemDate).Max(d => d.ItemPrice));
  }

您需要一个if-else块,为什么不将
传递到
另一个函数
中,而不是自己指定所有值呢。另外,由于缺少大括号,您提供的代码无法编译,请正确显示它。这是来自平面文件的数据,但我可能能够处理数据的导入。好主意,这与我的答案类似,但请注意,
ItemPrice
是一个字符串而不是int,因此需要进行转换。您是对的,几秒钟前的地方:-)我知道我没有抱怨复制:)根据我的答案和其他一些答案,
ItemPrice
变量是一个字符串,因此将可能需要转换为INT。在我给定的场景中,此建议最适合我!非常感谢你!如果需要,请参阅我的答案,了解int的转换逻辑