2
\$\begingroup\$

I have extension methods to encapsulate query conditions, however I have to separate IQueryable and IEnumerable. Is there any way I can reduce the redundancy and still not lose the performance benefit of IQueryable?

public static class PAYTRACK_PARTNER_Extension 
{
    public static IQueryable<PAYTRACK_PARTNER> WhereInPayer(this IQueryable<PAYTRACK_PARTNER> query, int payerId)
    {
        var result = query.Where(q => q.payer_id == payerId);
        return result;
    }

    public static IEnumerable<PAYTRACK_PARTNER> WhereInPayer(this IEnumerable<PAYTRACK_PARTNER> query, int payerId)
    {
        var result = query.Where(q => q.payer_id == payerId);
        return result;
    }

    public static IQueryable<PAYTRACK_PARTNER> WhereInPayee(this IQueryable<PAYTRACK_PARTNER> query, int payeeId)
    {
        var result = query.Where(q => q.payee_id == payeeId);
        return result;
    }

    public static IEnumerable<PAYTRACK_PARTNER> WhereInPayee(this IEnumerable<PAYTRACK_PARTNER> query, int payeeId)
    {
        var result = query.Where(q => q.payee_id == payeeId);
        return result;
    }


    public static IQueryable<PAYTRACK_PARTNER> WhereIsNotApproved(this IQueryable<PAYTRACK_PARTNER> query)
    {
        var result = query.Where(q => q.partner_is_approve.GetValueOrDefault(false).Equals(false));
        return result;
    }

    public static IEnumerable<PAYTRACK_PARTNER> WhereIsNotApproved(this IEnumerable<PAYTRACK_PARTNER> query)
    {
        var result = query.Where(q => q.partner_is_approve.GetValueOrDefault(false).Equals(false));
        return result;
    }

    public static PAYTRACK_PARTNER SingleByPayerAndPayee(this IQueryable<PAYTRACK_PARTNER> query, int payerId, int payeeId)
    {
        var result = query.Single(q => q.payer_id == payerId && q.payee_id == payeeId);
        return result;
    }

    public static PAYTRACK_PARTNER SingleByPayerAndPayee(this IEnumerable<PAYTRACK_PARTNER> query, int payerId, int payeeId)
    {
        var result = query.Single(q => q.payer_id == payerId && q.payee_id == payeeId);
        return result;
    }
}
\$\endgroup\$
4
\$\begingroup\$

You can use IQueryable<T> everywhere, because you can convert to it from IEnumerable<T> by using AsQueryable():

public static IQueryable<PAYTRACK_PARTNER> WhereInPayer(
    this IQueryable<PAYTRACK_PARTNER> query, int payerId)
{
    var result = query.Where(q => q.payer_id == payerId);
    return result;
}

public static IEnumerable<PAYTRACK_PARTNER> WhereInPayer(
     this IEnumerable<PAYTRACK_PARTNER> query, int payerId)
{
     return query.AsQueryable().WhereInPayer(payerId);
}

Also, names in ALL_CAPS are not commonly used in C#, you should adopt c# naming conventions.

\$\endgroup\$
  • \$\begingroup\$ thank you :) i can't change to all cap though because it is dbml generated from db and they named it like that. \$\endgroup\$ – Sarawut Positwinyu Jul 9 '13 at 12:15
  • \$\begingroup\$ I can't convert it in linq to entity. like company.T_RFQs when T_RFQs is entity set. i just can't convert to by asQueryable. compiler said it is not support :( \$\endgroup\$ – Sarawut Positwinyu Oct 3 '13 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.