score:0

this is little tricky, but this is how we are doing it. you can combine multiple iqueryables into one as shown below.

public static iqueryable<order> activeordersquery(this mydbentities db)
{
    return db.orders.where(
           o=> o.active && 
               (o.transactiontype == transactiontype.order ||
                o.transactiontype == transactiontype.subscription )));

}

public iqueryable<userview> getview()
{
    var q = context.activeordersquery();
    return context.users.select( x=> new userview{
         id = x.id,
         active = q.any( o=> o.userid == x.id)
    });
}

the only problem is you have to explicitly compare foreign keys. but this results in exactly same sql and will perform same.

this way you can refactor common queries and use them inside your views.

score:2

if you want to use your method in a lot of places, i don't think you can do anything except store it as an expression and then referring to that expression wherever you were calling the original method:

public static expression<func<user, bool>> isuseractive = user =>
    user.orders.any(c => c.active &&
                         (c.transactiontype == transactiontype.order ||
                          c.transactiontype == transactiontype.subscription));

public iqueryable<userview> getview()
{
    return context.users.select(p => new userview
        {
            id = p.id,
            active = isuseractive(p)
        });
}

edit: ok, so this doesn't work. you can't just "call" an expression like this. the only alternative i can think of is to turn your whole select parameter into an expression:

public static expression<func<user, userview>> usertouserview = user =>
    new userview
        {
            id = user.id,
            active =  user.orders.any(c => c.active &&
                         (c.transactiontype == transactiontype.order ||
                          c.transactiontype == transactiontype.subscription)
        };

public iqueryable<userview> getview()
{
    return context.users.select(usertouserview);
}

Related Query

More Query from same tag