Click here to Skip to main content
15,917,928 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
database is like this
item_id		int
list_name	string
category_name	string
item_name	string
qty		int
price		double
total_amt	double

i am trying to get item list according to category..(group by & sum)
C#
 private ObservableCollection<shoppingitem> _purchased;
        public ObservableCollection<shoppingitem> Purchased
        {
            get
            {
                return _purchased;
            }
            set
            {
                _purchased = value;
                notifypropertychanged("Purchased");
            }
        }
public void  budgetcategorywise()
        {


           
            var q = from shoppingItem p in db.Item1
                    group p by p.category_name into g
                    select new { category = g.Key, total = g.Sum(p => p.total_amt) };

	    Purchased=new ObservableCollection<shoppingitem>(q);
	}

it is giving error of type casting.
now the problem is how i should bind this result to listbox.
it requires conversion..?

i need to convert the result in observablecollection to bind with listbox..
Posted
Updated 31-May-12 19:34pm
v2

this is syntax to convert type casting...this may helps to u.

XML
public static class ObservableExtensions
    {
        public static ObservableCollection<T> ToObservableCollection<T>(this List<T> items)
        {
            ObservableCollection<T> collection = new ObservableCollection<T>();

            foreach (var item in items)
            {
                collection.Add(item);
            }

            return collection;
        }
    }
 
Share this answer
 
pls try like this

XML
private ObservableCollection<shoppingitem> _purchased;
        public ObservableCollection<shoppingitem> Purchased
        {
            get
            {
                return _purchased;
            }
            set
            {
                _purchased = value;
                notifypropertychanged("Purchased");
            }
        }
public static class  budgetcategorywise()
        {
            var q = from shoppingItem p in db.Item1
                    group p by p.category_name into g
                    select new { category = g.Key, total = g.Sum(p => p.total_amt) };
        
           ObservableCollection<q> collection =new ObservableCollection<q>();
{
foreach( var shoppingItem in shoppingItems)
{
collection.Add(shoppingItem);
}
}
return collection;
      }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900