LinqToSql Select to a class then do more queries

Posted by fyjham on Stack Overflow See other posts from Stack Overflow or by fyjham
Published on 2010-04-23T04:03:23Z Indexed on 2010/04/23 4:13 UTC
Read the original article Hit count: 282

Filed under:
|
|
|

I have a LINQ query running with multiple joins and I want to pass it around as an IQueryable<T> and apply additional filters in other methods.

The problem is that I can't work out how to pass around a var data type and keep it strongly typed, and if I try to put it in my own class (EG: .Select((a,b) => new MyClass(a,b))) I get errors when I try to add later Where clauses because my class has no translations into SQL. Is there any way I can do one of the following:

  1. Make my class map to SQL?
  2. Make the var data-type implement an interface (So I can pass it round as though it's that)?
  3. Something I haven't though of that'll solve my issue?

Example:

public void Main()
{
    using (DBDataContext context = new DBDataContext())
    {
      var result = context.TableAs.Join(
         context.TableBs,
         a => a.BID,
         b => b.ID,
        (a,b) => new {A = a, B = b}
      );
      result = addNeedValue(result, 4);
   }
}

private ???? addNeedValue(???? result, int value)
{
    return result.Where(r => r.A.Value == value);
}

PS: I know in my example I can flatten out the function easily, but in the real thing it'd be an absolute mess if I tried.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-sql