我的目标是将 IEnumerable 的船只按组拆分。 船舶群是指彼此之间相距在 0.5 海里以内的船舶。
UPD:假设我们有一条河流,例如密西西比河。船只在其中漂浮。 我的目标是根据他们的位置对他们进行分组。不是坐标,而是英里(例如 19.3 英里),因此,如果我们有一艘船位于 19.3 英里,而另一艘船位于 19.5 英里,我们会将它们分为一组。但是,如果我们在 193 英里处还有另一艘船,我们就无法将其添加到我们刚刚分组的两艘船的组中。
我们拥有的船舶集合中的未分组船舶:(所有河流和地点) IE可枚举。
我想知道如何在查询中考虑里程比较?
var orderedShips = from ship in ungroupedShips
orderby string.Intern(ship.River), ship.MileMarker ascending
group ship by string.Intern(ship.River) into shipRiverGroup
select new BargeGroup { Ships = shipRiverGroup };
最佳答案
我不明白你的 linq,但基于你的问题:
您可以使用截断按数字间隔进行分组,例如:
context.YourTable
.GroupBy(g => (int)g.YourFieldDouble)
.OrderBy(o => o.Key);
这将按整数值分组
如果你想分成 0.5 到 0.5 组,只需使用乘法和除法,例如:
context.YourTable
.GroupBy(g => (int)(g.YourFieldDouble * 2) / 2.0)
.OrderBy(o => o.Key);
您也可以使用舍入方法:
context.YourTable
.GroupBy(g => Math.Round((double)g.YourFieldDouble * 2,
MidpointRounding.AwayFromZero) / 2.0)
.OrderBy(o => o.Key)
关于c# - LINQ 考虑通过 double 比较元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33739080/