0

I have entity data-source on which i have used Query-created event to get Total amount but when I bind gridview it Gives Exception

An instance of ObjectQuery for unexpected result type '<>f__AnonymousType3`5' was assigned to Query in the QueryCreated event. An ObjectQuery for a result type compatible with 'purchase' was expected.

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
                  };
Akash
  • 125
  • 1
  • 3
  • 14
  • Please read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and give us more information about your code and what you have tried so far. – Peter Mar 07 '16 at 13:41

1 Answers1

0

Cast your query ToList() before binding to the Gridview.

    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();
James Dev
  • 2,979
  • 1
  • 11
  • 16
  • You can't convert Anonymous type into list . Nice try – Akash Mar 07 '16 at 13:48
  • Of course you can. It's probably because you are assigning it incorrectly to e.Query. Check the update answer you can bind that new variable to your gridview. – James Dev Mar 07 '16 at 13:50
  • Error 28 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) – Akash Mar 07 '16 at 13:53
  • Don't assign it to e.Query. Check my updated answer. – James Dev Mar 07 '16 at 13:54
  • I'm using it on EntityDataSource1_QueryCreated event Otherwise i have done it before posting here . it create eror on Query created event – Akash Mar 07 '16 at 13:55