tags:

views:

30

answers:

3

Hello,

What should i return from a WCF service when using Linq?

Eg :-

var res = from q in context.cust select q;

The Linq follows Deferred Execution and thus the statement doesn't do anything until a for loop is ran. This means i can't just return res. Then what should i return? Do i need to write a for loop and populate objects and return it's List everytime i want to return data from WCF service? Is there no equivalent of ADO.NET DataSet which follows disconnected architecture and is ideal for moving data between different tiers and from web service or WCF service?

Thanks in advance :)

+1  A: 

Execution is deferred until the data is actually requested, you're right that that may be a for loop but it would also be when it needs to be serialized for transfer via WCF. If you make your WCF service return IEnumerable then the query will be executed and the results returned.

Dave
+1  A: 

If you return it as IEnumerable<cust> the WCF serializer should execute the query.

But I usually do

var res = (from q in context.cust select q).ToList();
Albin Sunnanbo
+1  A: 

Because of context mentioned in your query you have to execute query explicitly by converting result to array or list. Context is probably related to Linq-To-Sql or EF and there is a big chance that during serialization the deffered execution fails because the context will be already closed.

Ladislav Mrnka