0

i wanted to ask if it possible to select an entity database table through a webget parameter ? here is what it might look like :

[WebGet]
    public IQueryable<TestTable> GetAllCallers(string select, string **table**)
    {

        //testCDREntities context = this.CurrentDataSource;

            var Callers = from d in this.CurrentDataSource.**table**
                          select d ;

        return Callers;



    }

ofcourse this doesn't work but is there a way to let this work ? I hope somebody can help me with this :)

1 Answers1

0

This is impossible. You can't serialize IQueryable<T>, therefore you can't send it trough WCF.

The usual way is to pack the properties you need into a serializable Data Transfer Object. We are using Automapper to map our domain entities to DTOs.

If you are hardcore, take a look at this discussion about serializing func.

It could however be possible to serialize TestTable[]. TestTable must be a known class that can be instantiated (i.e. not abstract, not an interface). Also all properties used in the datacontract must be serializable.

return Callers.ToArray();

.ToArray() executes your query, so now you are sending the actual data, not an abstract syntax tree.

--Edit--

Sorry, I was totaly wrong about the impossibility of sending IQueryable over the wire. I totally missed that we are talking about an OData service here. I was thinking about SOAP services all the time. Thanks Rytmis for clearing that up. Googling with the correct search terms also turned up this discussion: Expose IQueryable Over WCF Service

Community
  • 1
  • 1
Georg Patscheider
  • 8,808
  • 1
  • 21
  • 34
  • Sorry i typed it wrong but can you select a table from a db through parameters ? – lesley belgium cuber houben Apr 07 '16 at 07:34
  • You could use reflection to get the table type from its name, and then use this type in `dbContext.Set()`. However, it is not possible to return a generic type through WCF. You could however introduce some base type for your tables that contains the common data and return this base type. – Georg Patscheider Apr 07 '16 at 10:05
  • No, it's not impossible. WCF Data Services maps IQueryables to OData queries. See https://msdn.microsoft.com/en-us/library/dd744841(v=vs.110).aspx. – Rytmis Apr 07 '16 at 20:32