adeelmahmood (1) [Avatar] Offline
hi there
I have a simple function that returns a IQueryable type ..
function IQueryable<TableA> GetTypes(){
return from a in dc.TableA
select a
this works fine and gives me back all the columns of the table .. if I want to only get some columns back .. I change my select to
select new {, a.type}
I get an error message
cannot implcitly convert IQueryable<Anonymous Type#1> to IQueryable<TableA>

so I changed my select statement to
(from ... select new {, a.type}).AsQueryable<TableA>()
then I got this error message:
'System.Linq.IQueryable<AnonymousType#1>' does not contain a definition for 'AsQueryable' and the best extension method overload 'System.Linq.Queryable.AsQueryable<TElement>(System.Collections.Generic.IEnumerable<TElement>smilie' has some invalid arguments

can someone plz point out wat I am doing wrong
fabrice.marguerie (224) [Avatar] Offline
Re: multiple columns from linq as IQueryable

It's not possible to return an anonymous type (or a collection of an anonymous type) from a method. Unless you declare the return type to be Object.
It's not possible either to convert an instance of an anonymous type to TableA. There is no inheritence relation between them.

What you want to do here is declare a new class that contains the properties you want to keep (here, "id" and "type"). You'll then be able to declare your method as returning an IQueryable of that class.

Here is an example:

public class CustomClass
public Int32 ID { get; set; }
public String Type { get; set; }


function IQueryable<CustomClass> GetTypes()
from a in TableA
select new CustomClass{ ID =, Type = a.type });