我想總結一個泛型集合中的值,我已經使用相同的確切代碼在我的其他代碼中執行此功能,但它似乎有一個問題數據類型爲ulong
?在LINQ中使用求和方法
代碼
Items.Sum(e => e.Value);
有以下錯誤:
Error 15 The call is ambiguous between the following methods or properties: '
System.Linq.Enumerable.Sum<System.Collections.Generic.KeyValuePair<int,ulong>>(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<int,ulong>>, System.Func<System.Collections.Generic.KeyValuePair<int,ulong>,float>)
' and 'System.Linq.Enumerable.Sum<System.Collections.Generic.KeyValuePair<int,ulong>>(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<int,ulong>>, System.Func<System.Collections.Generic.KeyValuePair<int,ulong>,decimal?>
)
public class Teststuff : BaseContainer<int, ulong, ulong>
{
public decimal CurrentTotal { get { return Items.Sum(e => e.Value); } }
public override void Add(ulong item, int amount = 1)
{
}
public override void Remove(ulong item, int amount = 1)
{
}
}
public abstract class BaseContainer<T, K, P>
{
/// <summary>
/// Pass in the owner of this container.
/// </summary>
public BaseContainer()
{
Items = new Dictionary<T, K>();
}
public BaseContainer()
{
Items = new Dictionary<T, K>();
}
public Dictionary<T, K> Items { get; private set; }
public abstract void Add(P item, int amount = 1);
public abstract void Remove(P item, int amount = 1);
}
謝謝我只是將所有的超標改爲十進制,我會盡快接受這個計時器。 – lakedoo
@lakedoo:請注意,如果您更改「值」的類型,則不需要投射。另外,你確定你不想「長」嗎? – SLaks
好點,是的,我很長一段時間似乎沒有任何問題。再次感謝! – lakedoo