如何将数据转换为可观察的集合linq [英] how to convert data to obsevable collection linq

查看:142
本文介绍了如何将数据转换为可观察的集合linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库就是这样

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)

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


它给出类型转换错误.
现在的问题是我应该如何将此结果绑定到列表框.
它需要转换..?

我需要将observablecollection中的结果转换为与列表框绑定..


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..

推荐答案

这是转换类型转换的语法...这可能对您有所帮助.

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

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


pls像这样尝试

pls try like this

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


这篇关于如何将数据转换为可观察的集合linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆