日期:2014-05-17 浏览次数:21299 次
static void Main(string[] args) { List<Tuple<int, double>> lists = new List<Tuple<int, double>>(); for (int i = 0; i < 24; i++) { lists.Add(new Tuple<int, double>(i,i)); } Console.WriteLine(lists.Where((u) => u.Item1 >= 0 && u.Item1 <= 4).Sum((u) => u.Item2)); Console.WriteLine(lists.Where((u) => u.Item1 >= 5 && u.Item1 <= 17).Sum((u) => u.Item2)); Console.WriteLine(lists.Where((u) => u.Item1 >= 18 && u.Item1 <= 23).Sum((u) => u.Item2)); }
------解决方案--------------------
List<double> lists = new List<double>(); for (int i = 0; i < 24; i++) { lists.Add(i); } Console.WriteLine(lists.Take(5).Sum()); Console.WriteLine(lists.Skip(5).Take(13).Sum()); Console.WriteLine(lists.Skip(18).Sum());
------解决方案--------------------
ls正解
再给一个办法
Console.WriteLine(lists.Select((x, i) => new { x, i }).Where(x => x.i >= 5 && x.i <= 13).Sum(x => x.x));
------解决方案--------------------
代码如下:
List<double> lists = new List<double>();
for (int i = 0; i < 24; i++)
{
lists.Add(i);
}
Response.Write("索引为0~4的结果为:" + lists.Take(5).Sum() + "</br>");
Response.Write("索引为5~17的结果为:" + lists.Skip(5).Take(13).Sum() + "</br>");
Response.Write("索引为18~23的结果为:" + lists.Skip(18).Sum() + "</br>");
结果如下:
0~4的结果为:10
5~17的结果为:143
18~23的结果为:123
------解决方案--------------------
Linq 都玩的很溜啊,,,我Linq都不会。。