LINQ query "Select" will not work with columns specified

Last post 06-05-2008 2:16 PM by ecbruck. 7 replies.

Sort Posts:

  • LINQ query "Select" will not work with columns specified

    06-02-2008, 12:52 PM
    • Member
      20 point Member
    • MDeibert
    • Member since 02-01-2007, 5:56 PM
    • Near Baltimore/Washington
    • Posts 72

    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! :-)

     

    Filed under:
  • Re: LINQ query "Select" will not work with columns specified

    06-02-2008, 1:35 PM
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator

    Your select states that you are not returning the entire "Customer" object, but just the "CompanyName" field. If that field is of type "string", then you can simply return a type of List<string>. 

    public List<string> GetDropDownOptions_Customers()
    {
    	return (from c in DL.Customers
    			where c.Relationship == "Customer"
    			orderby c.CompanyName
    			select c.CompanyName).ToList();
    }
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: LINQ query "Select" will not work with columns specified

    06-04-2008, 11:05 AM
    • Member
      20 point Member
    • MDeibert
    • Member since 02-01-2007, 5:56 PM
    • Near Baltimore/Washington
    • Posts 72

    I'm still getting the runtime errors. I can't seem to be able to cast the LINQ query results into anything. Here is my test method that is failing...

    public List<string> GetDropDownOptions_Customers()
    {
         return (List<string>) (
         from c in DL.Customers
         where c.Relationship == "Customers"
         orderby c.CompanyName
         select new { CompanyName = c.CompanyName }
         );
    }

    I need the "CompanyName" in the select to use in the DataTextField property in a DropList. Follow?

    Replace the "List<string>" above with anything you want IEnumerable, IQueryable, whatever and I get the same casting error at runtime.

    Adding ".ToList<string>()" at the end of the query produces same results. 

    What am I doing wrong?

    Thanks for your help guys Smile

     

  • Re: LINQ query "Select" will not work with columns specified

    06-04-2008, 11:20 AM
    Answer
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator

    I just tested something similar, and this should work for you: 

    public IEnumerable GetDropDownOptions_Customers()
    {
    	return from c in DL.Customers
    		   where c.Relationship == "Customers"
    		   orderby c.CompanyName
    		   select new { CompanyName = c.CompanyName };
    }
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: LINQ query "Select" will not work with columns specified

    06-04-2008, 11:38 AM
    • Member
      20 point Member
    • MDeibert
    • Member since 02-01-2007, 5:56 PM
    • Near Baltimore/Washington
    • Posts 72

    IEnumerable would not build however, you did get me on track! This works perfectly...

    public IQueryable GetDropDownOptions_Customers()
    {
         return
         (
              from c in DL.Customers
              where c.Relationship == "Customers"
              orderby c.CompanyName
              select new { CompanyName = c.CompanyName }
         );
    }

    I made prior attempts with the method return type "IQueryable<string>" and that always failed. Can you explain the difference? I'm happy this works, but I don't know why.

    Thank you!!!! Big Smile

  • Re: LINQ query "Select" will not work with columns specified

    06-04-2008, 11:52 AM
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator

    Using Select New, you're not returning just a string, instead you're returning a typed object. I'm not sure why thought that returning a data type of IEnumerable didn't work for you.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: LINQ query "Select" will not work with columns specified

    06-05-2008, 8:37 AM
    • Member
      20 point Member
    • MDeibert
    • Member since 02-01-2007, 5:56 PM
    • Near Baltimore/Washington
    • Posts 72

    I guess the puzzle for me is exactly what object does....

            select new { CompanyName = c.CompanyName }

    return and then to what objects can it be cast?

    I found more than a few LINQ queries at MS site that were returning IEnumerable<???> so I figured that would work. But it never would build for me if I used the above Select with named columns.

    I'm a total LINQ rookie, as you can tell. Just trying to get my head around it all.

  • Re: LINQ query "Select" will not work with columns specified

    06-05-2008, 2:16 PM
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator

    WIth the select statement as you have, this returns what is called an Anonymous Type which is exactly why "var" was created. You could create your own type and then create an instance of that type in your Linq statement. Then, you could return a List of your new type. Otherwise, you pretty much have to use IEnumerable or IQueryable.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

Page 1 of 1 (8 items)