C#。内部连接到dataGrid,包装

时间:2015-10-28 01:30:38

标签: c# wpf linq datagrid

为了在dataGrid中显示我的Linq查询,我使用了以下代码中看到的包装,但它确实无效。

我仍然有这个错误cannot convert from 'AnonymousType#1' to ViewModels.ConsultInvoiceViewModel.SubInvoice'

我的匿名类型是 ' a是新的{Invoice Invoice,string description,string unit1}

为什么?

        private void GetSub()
        {
            var invoice = Selected;
            var invs = (from i in ctx.Invoices
                        join p in ctx.products on i.refSupp equals p.refsup
                        join u in ctx.units on p.unit equals u.idunit
                        where i.suppInvNumber == invoice.suppInvNumber
                        select new { 
                           Invoice = i,
                           description = p.description,
                           unit1 = u.unit1
                       });

             Products.Clear();
             if (invs != null)
                foreach (var inv in invs)
                {
                    Products.Add(inv); // error
                }
        }

        private ObservableCollection<SubInvoice> _products;

        public ObservableCollection<SubInvoice> Products
        {
            get
            {
                return _products;
            }
            set
            {
                _products = value;
                OnPropertyChanged("Products");
            }
        }

        public class SubInvoice
        {
            public Invoice invoice;
            public string description { get; set; }
            public string unit1 { get; set; }

        }

1 个答案:

答案 0 :(得分:1)

创建强类型集合

...
select new SubInvoice{
               Invoice = i,
               description = p.description,
               unit1 = u.unit1
          });

并使invoice属性

public Invoice Invoice {get; set;}