List<dynamic> list1 = new List<dynamic>();
list1.Add(new { id = "1", num = 11, time = "2015-10-10 10:10:10" });
list1.Add(new { id = "2", num = 22, time = "2015-10-10 11:11:11" });
list1.Add(new { id = "3", num = 33, time = "2015-10-10 12:12:12" });
List<dynamic> list2 = new List<dynamic>();
list2.Add(new { id = "11", num = 18, time = "2015-10-10 10:10:10" });
list2.Add(new { id = "21", num = 10, time = "2015-10-10 11:11:11" });
list2.Add(new { id = "3", num = 55, time = "2015-10-10 12:12:12" });
有上面2个数组,以两个数组的时间相等,判断出2个num值偏差比较大的那条数据,如上列2列要返回 list1.Add(new { id = "3", num = 33, time = "2015-10-10 12:12:12" });
static void Main(string[] args) { List<dynamic> list1 = new List<dynamic>(); list1.Add(new { id = "1", num = 11, time = "2015-10-10 10:10:10" }); list1.Add(new { id = "2", num = 22, time = "2015-10-10 11:11:11" }); list1.Add(new { id = "3", num = 33, time = "2015-10-10 12:12:12" }); List<dynamic> list2 = new List<dynamic>(); list2.Add(new { id = "11", num = 18, time = "2015-10-10 10:10:10" }); list2.Add(new { id = "21", num = 10, time = "2015-10-10 11:11:11" }); list2.Add(new { id = "31", num = 55, time = "2015-10-10 12:12:12" }); var maxId = ""; var maxAbs = 0; list1.ForEach(p => { var numList = list2.FindAll(c => c.time == p.time).Select(c=>c.num); var maxNum = numList.Max(); var minNum = numList.Min(); if (Math.Abs(p.num - maxNum) > Math.Abs(p.num - minNum)) { if (maxAbs < Math.Abs(p.num - maxNum)) { maxAbs = Math.Abs(p.num - maxNum); maxId = p.id; } } else { if (maxAbs < Math.Abs(p.num - minNum)) { maxAbs = Math.Abs(p.num - minNum); maxId = p.id; } } }); var max= list1.Find(p => p.id == maxId); Console.WriteLine("结果是 id={0},num={1},time={2}", max.id, max.num, max.time); Console.ReadKey(); }
这样好像能达到你的要求. 没有仔细的测试过..仅供参考..
可以通过linq表达式查询。
具体点的代码呢~
@菜菜灰: 没看懂题目要求。。
参考下: http://bbs.csdn.net/topics/390233316