我有4个dutPdItems的集合。我想找到TotTfpRig最低的DutPdSeqNum来包含重复项。
我想产生一个linq查询,该查询找到最小值的项目编号。该查询应返回一个列表。
如果有重复项(如上所示),则应包括等于最小值的所有重复项编号。查询应该返回一个列表。
查询应返回一个int列表,该列表中包含DutPdSeqNum 2和3。
List<DutPdItem> dutPdItemList = new List<DutPdItem>();
DutPdItem dutPd1 = new DutPdItem()
{
DutPdSeqNum = 1,
FltTfpTot = 6.4m,
RigDpMin = 0.0m,
RigDhr = 0.0m,
RigAdg = 0.0m,
RigThr = 0.0m
};
DutPdItem dutPd2 = new DutPdItem()
{
DutPdSeqNum = 2,
FltTfpTot = 4.0m,
RigDpMin = 0.0m,
RigDhr = 1.55m,
RigAdg = 0.0m,
RigThr = 0.0m
};
DutPdItem dutPd3 = new DutPdItem()
{
DutPdSeqNum = 3,
FltTfpTot = 4.0m,
RigDpMin = 0.0m,
RigDhr = 1.55m,
RigAdg = 0.0m,
RigThr = 0.0m
};
DutPdItem dutPd4 = new DutPdItem()
{
DutPdSeqNum = 4,
FltTfpTot = 6.3m,
RigDpMin = 0.0m,
RigDhr = 0.0m,
RigAdg = 0.0m,
RigThr = 0.0m
};
dutPdItemList.Add(dutPd1);
dutPdItemList.Add(dutPd2);
dutPdItemList.Add(dutPd3);
dutPdItemList.Add(dutPd4);
dutPdItemList.Dump("DutyPeriod Item List");
var THRtoDistribute = 2.715m;
// gets the lowest duty period by total pay
var lowestDutyPeriodsByTotPay = dutPdItemList.GroupBy(x => x.TotTfpRig)
.Where(g => g.Count() >= 2)
.Select(y => y.Key);
// this query does not work, I cannot figure our why
var lowestDutyPeriod = dutPdItemList.Select(dp => dp.TotTfpRig ==
lowestDutyPeriodsByTotPay);
lowestDutyPeriodsByTotPay.Dump("lowest duty period");
}
class DutPdItem
{
public int DutPdSeqNum { get; set; }
public decimal FltTfpTot { get; set; }
public decimal RigDpMin { get; set; }
public decimal RigDhr { get; set; }
public decimal RigAdg { get; set; }
public decimal RigThr { get; set; }
public decimal TotTfpRig => FltTfpTot + RigDpMin + RigDhr + RigAdg +
RigThr;
我已经弄清楚了如何创建linq查询。
这里是:
var minTfp = dutPdItemList.Min(dp => dp.TotTfpRig).Dump("Min Tfp");
var minDutPd = dutPdItemList.Where(dp => dp.TotTfpRig == minTfp).Select(dp => dp.DutPdSeqNum).ToList().Dump("Min Duty Pds");
var countMin = minDutPd.Count().Dump("count minDutPd");
var secondLowestTfp = dutPdItemList.OrderBy(dp => dp.TotTfpRig).Select(dp => dp.TotTfpRig).Skip(countMin).First().Dump("2nd Lowest Tfp");
var secondLowestDutPd = dutPdItemList.OrderBy(dp => dp.TotTfpRig).Select(dp => dp.DutPdSeqNum).Skip(countMin).First().Dump("2nd Lowest Duty Pd");
答案 0 :(得分:1)
public struct IdAndValue { // Example data structure
public int id;
public double val;
}
static IEnumerable<IdAndValue> findAllMinimums(ICollection<IdAndValue> data)
{
var byValue = data.ToLookup(d => d.val);
return byValue[byValue.Min(grp => grp.Key)];
}
要获取列表,请执行findAllMinimums(...).ToList()
。