WP7 LINQ to SQL和聚合查询
本文关键字:查询 SQL LINQ to WP7 | 更新日期: 2023-09-27 18:21:21
我在WP7应用程序中添加了以下代码块,但不确定错误的含义;
private void GetTodaysTotal()
{
// Define the query to gather all of the to-do items.
var boughtItemsToday = (from DBControl.MoneySpent
bought in BoughtItemDB.BoughtItems
select bought.ItemAmount).Sum();
// Execute the query and place the results into a collection.
BoughtItemsToday = new ObservableCollection<DBControl.MoneySpent> boughtItemsToday);
}
我的错误就在眼前;
BoughtItemsToday = new ObservableCollection<DBControl.MoneySpent> boughtItemsToday);
并且是;
与"System.Collections.ObjectModel.OObservableCollection.OObservable Collection(System.Collections.Generic.List)"匹配的最佳重载方法具有一些无效参数
我知道这与LINQ查询返回十进制值有关,但我不知道如何修复它。我打算将结果绑定到XAML TextBlock中。
你说得对,事实是你从第一个查询中返回了一个小数,然后试图将这些小数强制转换为DBControl.MoneySpent
对象。您需要两个单独的查询。
想想这个。您的第一个查询将只得到DBControl.MoneySpent
对象:
var boughtItemsToday = (from DBControl.MoneySpent
bought in BoughtItemDB.BoughtItems
select bought);
然后,您可以创建您的可观察集合,如下所示:
var BoughtItemsToday = new ObservableCollection<DBControl.MoneySpent>(boughtItemsToday);
最后,只需单独获得您的金额:
var sum = boughtItemsToday.Sum(item => item.ItemAmount);