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# 使用LINQ在对象中查找Min()的多个实例并更改元素_C#_Linq_List - Fatal编程技术网

C# 使用LINQ在对象中查找Min()的多个实例并更改元素

C# 使用LINQ在对象中查找Min()的多个实例并更改元素,c#,linq,list,C#,Linq,List,我上过以下课程 public class PricePlan { public string Name { get; set; } public List<Price> Prices { get; set; } public PricePlan() { Prices = new List<Price>(); } } public class Price { public DateTime Date { g

我上过以下课程

public class PricePlan
{
    public string Name { get; set; }
    public List<Price> Prices { get; set; }

    public PricePlan()
    {
        Prices = new List<Price>();
    }
}

public class Price
{
    public DateTime Date { get; set; }
    public decimal Rate { get; set; }
    public bool Free { get; set; }

    public Price()
    {
        Free = false;
    }
}
公共类价格计划
{
公共字符串名称{get;set;}
公开标价{get;set;}
公共价格计划()
{
价格=新列表();
}
}
公共类价格
{
公共日期时间日期{get;set;}
公共十进制率{get;set;}
公共布尔自由{get;set;}
公共价格()
{
自由=错误;
}
}
然后按以下步骤填充对象和列表

PricePlan oPricePlan = new PricePlan();

oPricePlan.Name = "Standard Rate Plan";

Price oPrice;
DateTime oDate = DateTime.Today;

for (int x = 1; x < 10; x++)
{
   oPrice = new Price();
   oPrice.Date = oDate.AddDays(x);
   oPrice.Rate = 10 * x;
   oPricePlan.Prices.Add(oPrice);
}

oPrice = new Price();
oPrice.Date = oDate.AddDays(11);
oPrice.Rate = 10;
oPricePlan.Prices.Add(oPrice);
PricePlan oPricePlan=newpriceplan();
oPricePlan.Name=“标准费率计划”;
价格操作;
DateTime oDate=DateTime.Today;
对于(int x=1;x<10;x++)
{
oPrice=新价格();
操作日期=日期加天数(x);
操作率=10*x;
操作计划。价格。添加(操作);
}
oPrice=新价格();
操作日期=日期加天数(11);
手术率=10;
操作计划。价格。添加(操作);
示例数据可能是:

2013年1月2日,10日,假
2013年1月3日,20日,假
2013年1月4日,30日,假
2013年1月5日,40,假
2013年1月6日,50,假
2013年1月7日,60,假
2013年1月8日,70,假
2013年1月9日,80,假
2013年1月10日,90,假
2013年1月12日,10,假

使用

oPricePlan.Prices.Min(r=>r.Rate)

我得到了Rate或IndexOf[]可以返回第一个实例的最小值。然而,我想返回X个最低利率。例如,我如何设置以下内容

  • 对于系统中的1分钟速率(两个速率可能具有相同的Min),将其设置为0,将自由布尔设置为true

  • 对于2分钟的速率(这可能是相同的),将其设置为0,将自由布尔设置为true

所以基本上我想找到最低的X个速率,改变找到的实际最低速率,并将Free bool标志设置为true


我应该考虑使用LINQ,还是他们更喜欢的方式?

您可以使用
OrderBy
+
GroupBy
Take
和循环:

var priceGroups = oPricePlan.Prices
    .OrderBy(p => p.Rate)  // order by rate ascending
    .GroupBy(p => p.Rate)  // group by rate
    .First()  // use the lowest price-rate group only
    .Take(2); // change 2 to 1 if you only want to modify one price in this min-group

foreach (Price price in priceGroups)
{
    price.Rate = 0;
    price.Free = true;
}

包括示例输出以澄清您的问题。我得出了另一个回答者的不同结论。在这个例子中,最便宜的利率是10,所以我希望它们都设置为0(在2013年1月2日和2013年1月12日)。好的,如果你想删除
2
min利率,而不是
1
,这是指
10
20
还是两者都是
10
s?如果我更改了numberOfItems,我会选择两个最低的价格。在本例中,它是10和10。但可能是10和20你的原始答案似乎符合我的要求?我的原始答案只会将
10
中的一个评分项目设置为
0
,(因为
numberOfItems=1
)你说你希望两者都设置为
0
int numberOfItems = 1;
var orderedPrices = oPricePlan.Prices.OrderBy(x => x.Rate).ToList();
decimal targetRate = orderedPrices[numberOfItems - 1].Rate;
foreach (var price in orderedPrices.TakeWhile(x => x.Rate <= targetRate))
{
    price.Rate = 0;
    price.Free = true;
}
int numberOfItems = 1;
foreach (var price in oPricePlan.Prices.OrderBy(x => x.Rate).Take(numberOfItems))
{
    price.Rate = 0;
    price.Free = true;
}