Eric J.
Eric J.

Reputation: 150168

AsEnumerable and Query Syntax

I have a rather complex Linq expression written using query syntax. Simplified, it looks like:

var query = 
    from a in ctx.A
    from b in ctx.B
    where a.Bid == b.Id
    select new MyType()
    {
        Foo = a.Foo,
        Bar = b.Bar
    }

I have to modify the query to set a property on the new instance of MyType in a manner that cannot be evaluated by the query provider. The result set fairly small, so it is reasonable to use .AsEnumerable(). I found an example on MSDN that shows how to do this in a simple case

IEnumerable<DataRow> query =
    from product in products.AsEnumerable()
    select product;

How do I use AsEnumerable() in my more complex case? That is, how do I achieve something like

var query = 
    from a in ctx.A
    from b in ctx.B
    where a.Bid == b.Id
    AsEnumerableGoesHereSomehow
    select new MyType()
    {
        Foo = a.SomeClientSideCalculation(),
        Bar = b.Bar
    }

I don't want to perform the join and filtering client side.

Upvotes: 0

Views: 146

Answers (1)

Robert McKee
Robert McKee

Reputation: 21477

Feel free to reduce {a=a,b=b} to just the properties you actually need, but you can use this as a start:

var query =( 
    from a in ctx.A
    from b in ctx.B
    where a.Bid == b.Id
    select new 
    {
        a = a,
        b = b
    })
    .AsEnumerable()
    .Select(x=>new MyType {
      Foo=Something(x.a),
      Bar=x.b
    });

Upvotes: 2

Related Questions