Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq with WCF

I am looking for any examples or guides to using Linq over WCF (n-tier application). Please specify if you are showing something for Linq-to-SQL or Linq-to-entities. I would like to see usage examples for both.

I am wondering how things like deffered execution works over WCF (if it works at all)? Cyclic references support and so on...

Any information to make this a quick start guide to using Linq with WCF is helpful.

like image 958
Phobis Avatar asked Oct 01 '08 17:10

Phobis


2 Answers

There isn't any LINQ provider that I'm aware of for generic WCF-based queries. LINQ to ADO.NET Data Services, however, lets you query an Entity model over WCF/REST.

From Andy Conrad's blog:

    static void Main(string[] args)
    {
      var context=new WebDataContext("http://localhost:18752/Northwind.svc");

      var query = from p in context.CreateQuery<Product>("Products")
                  where p.UnitsInStock > 100
                  select p;

      foreach (Product p in query)
      {
        Console.WriteLine(p.ProductName+", UnitsInStock="+p.UnitsInStock);
      }
   } 
like image 152
Mark Cidade Avatar answered Sep 28 '22 13:09

Mark Cidade


You can add a Linq to SQL class to a WCF service. Then go to your datacontext in the Linq to SQL class and in the properties set Serialization Mode to Unidirectional.

The entities in your Linq to SQL class will now be available through the WCF service :)

like image 33
Eric Avatar answered Sep 28 '22 13:09

Eric