提供当前的顶部和高度,我想获得位于该区域的物体。基于列表中的项目位置,默认/自定义高度和场景定义。 我准备了一个解决方案,但遗憾的是效率低下。 你能帮助我改善表现吗?
我的低效功能:
static List<int> GetIndexes(double y, double sceneHeight)
{
int index1 = 0;
var dataIndexed = (from d in Data.Cast<object>() select new { Index = index1++});//IList casting Data.Cast<object>()
double top = 0;
double height = 0;
int index2 = 0;
var xr = (from di in dataIndexed
join definition in Definitions on di.Index equals definition.Index into X
from x in X.DefaultIfEmpty()
select new
{
Index = index2++,
Top = top += height,
Height = height = (x == null ? DefaultHeight : x.Height) //Default or Custom height
});
return (from i in xr where i.Top >= y && i.Top < y + sceneHeight select i.Index).ToList();
}
我的自定义对象定义:
public class ItemDefinition
{
public int Index { get; set; }
public double Height { get; set; }
}
测试
public static void Main()
{
CreateData();
CreateDefinitions();
double expectedTop = 30;
double expectedHeight = 201;
DateTime start = DateTime.Now;
List<int> indexes = GetIndexes(expectedTop, expectedHeight);
Console.WriteLine("Duration: " + (DateTime.Now - start).TotalMilliseconds.ToString() + " ms");
foreach(int index in indexes)
Console.WriteLine(index);
}