Akash
Akash

Reputation: 125

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

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

Upvotes: 0

Views: 225

Answers (1)

James Dev
James Dev

Reputation: 3009

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

Upvotes: 0

Related Questions