是否有任何一般和簡單的方法來做這個計算?
有了這個模型,沒有。
IMO,唯一可以改進的是可用性。
假設模型是這樣的
public enum TransactionType { Credit, Debit }
public class Transaction
{
public int ID { get; set; }
public DateTime Date { get; set; }
public decimal Value { get; set; }
public TransactionType Type { get; set; }
public bool IsCredit { get { return Type == TransactionType.Credit; } }
public int Account { get; set; }
}
我會把計算的輔助函數這樣
public static class TransactionUtils
{
public static IEnumerable<KeyValuePair<Transaction, decimal>> GetCreditInfo(this IEnumerable<Transaction> accountTransactions)
{
decimal credit = 0;
return from t in accountTransactions
orderby t.Date, t.ID
select new KeyValuePair<Transaction, decimal>(t, credit += t.IsCredit ? t.Value : -t.Value);
}
}
現在LINQ查詢可用於回答包括從原來的不同的問題該職位。
例如,我們把你的樣本數據
var transactions = new List<Transaction>
{
new Transaction { ID = 1, Date = new DateTime(2015, 07, 23), Value = 100, Type = TransactionType.Credit, Account = 1 },
new Transaction { ID = 2, Date = new DateTime(2015, 07, 28), Value = 350, Type = TransactionType.Credit, Account = 1 },
new Transaction { ID = 3, Date = new DateTime(2015, 08, 14), Value = 250, Type = TransactionType.Credit, Account = 1 },
new Transaction { ID = 4, Date = new DateTime(2015, 08, 30), Value = 180, Type = TransactionType.Credit, Account = 1 },
new Transaction { ID = 5, Date = new DateTime(2015, 09, 22), Value = 230, Type = TransactionType.Credit, Account = 1 },
new Transaction { ID = 6, Date = new DateTime(2015, 09, 28), Value = 230, Type = TransactionType.Debit, Account = 1 },
new Transaction { ID = 1, Date = new DateTime(2015, 07, 23), Value = 190, Type = TransactionType.Credit, Account = 2 },
new Transaction { ID = 2, Date = new DateTime(2015, 07, 28), Value = 350, Type = TransactionType.Credit, Account = 2 },
new Transaction { ID = 3, Date = new DateTime(2015, 08, 14), Value = 450, Type = TransactionType.Credit, Account = 2 },
new Transaction { ID = 4, Date = new DateTime(2015, 08, 30), Value = 100, Type = TransactionType.Debit, Account = 2 },
new Transaction { ID = 5, Date = new DateTime(2015, 09, 22), Value = 100, Type = TransactionType.Credit, Account = 2 },
};
回答原來的問題會是這樣
decimal maxCredit = 1000;
對於特定帳戶
int account = 1;
bool hasBonus = transactions
.Where(t => t.Account == account)
.GetCreditInfo().Any(info => info.Value >= maxCredit);
的所有賬戶
var bonusInfo = transactions.GroupBy(t => t.Account, (key, elements) => new
{
Account = key,
HasBonus = elements.GetCreditInfo().Any(info => info.Value >= maxCredit)
}).ToList();
其他
var maxCreditInfo = transactions.GroupBy(t => t.Account, (key, elements) => new
{
Account = key,
MaxCredit = elements.GetCreditInfo().Max(info => info.Value)
}).ToList();
var bonusTransactionInfo = transactions.GroupBy(t => t.Account, (key, elements) => new
{
Account = key,
BonusTransactions = elements.GetCreditInfo()
.Where(info => info.Key.IsCredit && info.Value >= maxCredit).ToList()
}).ToList();
等
可以使用SQL輕鬆地做到這一點。你試過什麼了? – mikeb
我試圖通過代碼而不是sql。但是在我看來,代碼變得有點混亂。我讀取所有與linq-to-entities的交易,然後迭代所有交易以決定是否應積累。 – Lorenzo
我不明白你的約束。爲什麼沒有考慮第二個帳戶?是否也有時間限制?你也應該簡化你的例子。您不需要六個示例交易來獲得您的觀點。 –