繁体   English   中英

当我放置Select new {}时,Linq查询在EntityDataSource1_QueryCreated事件上给出异常;

[英]Linq query giving exception on EntityDataSource1_QueryCreated Event when I put Select new{};

我有实体数据源,在该数据源上我已使用Query创建的事件来获取总金额,但是当我绑定gridview时,它会给出异常

在QueryCreated事件中,将意外结果类型为'<> f__AnonymousType3`5'的ObjectQuery实例分配给Query。 期望使用与“购买”兼容的结果类型的ObjectQuery。

DateTime dt = DateTime.Now.AddDays(-1);
int ids=DML.getid(txtpartyname.Text);
var pur = e.Query.Cast<purchase>();
        e.Query = from p in pur
                  where p.InvoiceNo == txtinvoice.Text && p.InvoiceDate > dt
                  orderby p.id descending
                  select new
                  {
                      p.Amount,
                      p.category,
                      p.description,
                      p.qty,
                      Total=p.qty*p.Amount
                  };

在绑定到Gridview之前,将查询强制转换为ToList()。

    var pur = e.Query.Cast<purchase>();
    var gridViewList = (from p in pur
              where p.InvoiceNo == txtinvoice.Text && p.InvoiceDate > dt
              orderby p.id descending
              select new
              {
                  p.Amount,
                  p.category,
                  p.description,
                  p.qty,
                  Total=p.qty*p.Amount
              }).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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