0

I have some linq query like this :

from x in from dbcontext
 join y in dbcontext on x.id equals y.id select new {x,y}

my problem is how I can send these kinds of query to server as an object here is my linq query is direct data-access I need to send them to my data-server but I need an object. how I can implement this ?

kamiar3001
  • 2,646
  • 4
  • 42
  • 78
  • 1
    I think this is a strange approach in the end. Isn't it better to have the logic server side and expose it as a specific webservice or several specific webservices, instead of exposing a general webservice which accepts as input the logic to perform? Are you trying to make a universal function? `public object DoMiracle(object[] input) { }` – Bazzz Apr 19 '11 at 08:02

3 Answers3

2

Badly. Linq query contains references to the context. You don't have the context on the client and even if you have it will be different instance on the server. Doing anything like that would require damn big effort. Also you are returning projection to anonymous type - another very complicated feature when doing it remotely.

This can be of course handled - WCF Data Services already did this. They have client side "proxy" of context, query is serialized as OData query string and expression tree is built from that string on the server side. They also supports projections but they don't support STEs. Try WCF Data Services.

If you don't like the idea of WCF Data Services you should expose a method for each such query on the server side and client must call that method remotely.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
1

firstly, IIRC you can't serialize Expressions, so firstly take a look at this post

then something along the lines of the following:

public List<ContactDetails> GetAllContactDetails( List<Guid> ContactIDs, Func<List<ContactDetails>, IEnumerable<ContactDetails>> filter )
{
    List<ContactDetails> result = new List<ContactDetails>();

    // get contacts as usual, fill result
    // ...

    return filter.Invoke( result ).ToList();
}

Usage:

GetAllContactDetails( idList, ( result ) =>
    from contact in result
    where contact.DateOfBirth > DateTime.Now.AddYears( -10 )
    select contact );

D.R

Community
  • 1
  • 1
Dead.Rabit
  • 1,965
  • 1
  • 28
  • 46
0

Since Expression Trees are not serializable, it cannot be easily done.

My data access layer is not built with EF, so my solution may or may not be helpful to you, but here it is anyway:
I parse the expression on client side to a self-made serializable expression (which has properties like "columns", "joins", "filters", etc), and on the serverside I create sql based on these expressions.
You can create expressions from them of course, and send them against an EF linq provider!

TDaver
  • 7,164
  • 5
  • 47
  • 94