日期:2014-05-18 浏览次数:20984 次
/* 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 */ class PointData { public int x { get; set; } public int y { get; set; } public int data { get; set; } } ... var data = new List<PointData> { new PointData() { x = 0, y = 0, data = 0 }, new PointData() { x = 1, y = 0, data = 0 }, new PointData() { x = 2, y = 0, data = 0 }, new PointData() { x = 3, y = 0, data = 1 }, new PointData() { x = 0, y = 1, data = 0 }, new PointData() { x = 1, y = 1, data = 1 }, new PointData() { x = 2, y = 1, data = 0 }, new PointData() { x = 3, y = 1, data = 0 }, new PointData() { x = 0, y = 2, data = 0 }, new PointData() { x = 1, y = 2, data = 0 }, new PointData() { x = 2, y = 2, data = 1 }, new PointData() { x = 3, y = 2, data = 0 }, new PointData() { x = 0, y = 3, data = 0 }, new PointData() { x = 1, y = 3, data = 0 }, new PointData() { x = 2, y = 3, data = 0 }, new PointData() { x = 3, y = 3, data = 0 } }; //寻找距离3,3最近的点 var result = data.OrderBy(x => (x.x - 3) * (x.x - 3) + (x.y - 3) * (x.y - 3)).Where(x => x.data == 1).First(); Console.WriteLine("result is {0},{1}.", result.x, result.y);