简体   繁体   中英

WP7 LINQ to SQL and Aggregate query

I have added the following block of code to my WP7 app and not sure what the error means;

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);
}

The error I am getting is on the line;

BoughtItemsToday = new ObservableCollection<DBControl.MoneySpent> boughtItemsToday);

and is;

The best overloaded method match for 'System.Collections.ObjectModel.ObservableCollection.ObservableCollection(System.Collections.Generic.List)' has some invalid arguments

I know this is something to with the LINQ query returning a decimal value, but I'm not sure how to fix it. I am intending on binding the result into a XAML TextBlock.

You're right, it is the fact that you are returning a decimal from your first query and then trying to cast those decimals to DBControl.MoneySpent objects. You will need 2 separate queries.

Consider this. Your first query will only get the DBControl.MoneySpent objects:

var boughtItemsToday = (from DBControl.MoneySpent 
                                        bought in BoughtItemDB.BoughtItems
                                        select bought);

Then, you can create your observable collection as follows:

var BoughtItemsToday = new ObservableCollection<DBControl.MoneySpent>(boughtItemsToday);

Finally, just get your sum separately:

var sum = boughtItemsToday.Sum(item => item.ItemAmount);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM