我有一个clas:
public class Item
{
public Item()
{
}
public string Id { get; set; }
public string Name { get; set; }
public decimal Value { get; set }
public decimal Average { get; set; }
}
如果我创建一个列表:
List<Item> items = new List<Item>();
如何设置这样的项目的平均值:
答案 0 :(得分:3)
你走了:
public static class helper {
public static IEnumerable<double> doSomething(this IEnumerable<int> source)
{
double sum = 0;
int count = 0;
foreach(var item in source)
{
sum += item;
count++;
yield return (sum / count);
}
}
}
测试:
var res = new List<int> { 1,2,3,4,5,6,7,8,9,10};
res.doSomething().Dump();
给出这个:
1
1.5
2
2.5
3
3.5
4
4.5
5
5.5
答案 1 :(得分:3)
var myvar = url('http://www.stackoverflow.com');
myvar = myvar.replace(/[url()]/g, '');
答案 2 :(得分:1)
for (int i = 0; i < items.Count; i++)
{
items[i].Average = items.Take(i + 1).Average(item => item.Value);
}