如何让我的视图返回集合的总和?

时间:2012-09-14 08:07:30

标签: c#

我尝试使用以下代码执行此操作,但它给了我很多错误:

public abstract class BaseGridViewModel
{

    protected BaseGridViewModel()
    {
        Events = new List<ViewEvent>();
        Watch = Stopwatch.StartNew();
    }
    public SelectList Statuses { get; set; }
    public IList<ViewEvent> Events { get; set; }
    public string Topics { get; set; }
    public SelectList Types { get; set; }
    public string View { get; set; }
    public Stopwatch Watch { get; set; }

    public void Event(string description) {
        if (Watch.IsRunning) {
            Events.Add(new ViewEvent(description, Watch.ElapsedMilliseconds));
        } else {
            throw new Exception("Watch not running");
        }
    }
    public long Elapsed {
        get { 
            return Events.Sum(event => event.Elapsed)
        }
    }

public class ViewEvent {

    public long Elapsed { get; private set; }
    public string Description { get; private set; }
    public string Message { get; private set; }
    public int Quantity { get; private set; }

    public ViewEvent(string description, long elapsedTime, int quantity = 0, string message = "") {
        this.Description = description; 
        this.Elapsed = elapsedTime;
        this.Quantity = quantity;
        this.Message = message;
    }

}

这一行的所有9个错误都是:“return Events.Sum(event =&gt; event.Elapsed)”

我的语法是否有问题?

1 个答案:

答案 0 :(得分:2)

event是关键字,请尝试e => e.Elapsed