I have DAL class with some methods for returning data to be used in ObjectDataSources. I'm just getting started on this and I have two methods. One that returns all columns in a query and one that tries to return specifice column names...
public List<Invoice> GetInvoicesByDate(DateTime dDateStart, DateTime dDateEnd)
{
var inv = from i in DL.Invoices
where (i.OrderDate >= dDateStart && i.OrderDate <= dDateEnd)
select i;
return (List<Invoice>)inv;
}
public List<Customer> GetDropDownOptions_Customers()
{
var cust = (from c in DL.Customers
where c.Relationship == "Customer"
orderby c.CompanyName
select new { c.CompanyName });
return (List<Customer>)cust;
}
When I try to bind the second method to a control, I run the project and get this error...
Unable to cast object of type 'System.Data.Linq.DataQuery`1[<>f__AnonymousType0`1[System.String]]' to type 'System.Collections.Generic.List`1[SCOSS.Data.Customer]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Data.Linq.DataQuery`1[<>f__AnonymousType0`1[System.String]]' to type 'System.Collections.Generic.List`1[SCOSS.Data.Customer]'.
Source Error:
Line 48: orderby c.CompanyName
Line 49: select new { c.CompanyName });
Line 50: return (List<Customer>)cust;
Line 51: }
Line 52: |
Can you decipher this error?
Where you see "List<Customer>", I have tried numerous other objects like IQueryable, IEnumerable, Table, and so on. Nothing works. Most won't build but the ones that do build always return the error above.
I'm pretty sure I have no understanding of the difference between the two methods above. Can somone post a quick correction to my second method and a brief explanation? I'm just spinning my wheels at this point.
Thank you! :-)