Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:08:17+00:00 2026-06-13T15:08:17+00:00

I’m writing a collection of LINQ 2 SQL extension methods. Right now, I have

  • 0

I’m writing a collection of LINQ 2 SQL extension methods. Right now, I have a bunch of duplicate methods, one working on an IEnumerable collection and the other on an IQueryable collection. For example

public static IQueryable<X> LimitVisible(this IQueryable<X> items) {
    return items.Where(x => !x.Hidden && !x.Parent.Hidden);
}

public static IEnumerable<X> LimitVisible(this IEnumerable<X> items) {
    return items.Where(x => !x.Hidden && !x.Parent.Hidden);
}

I think I need this duplication because I want to be able to retain the IQueryable behavior in some cases, so that execution is deferred to the database. Is this a correct assumption or would a single IEnumerable method (plus casting the output to IQueryable) work? If I do need both, I don’t like the duplication, so I want to combine the two using the generic method

public static T LimitVisible<T>(this T items) where T : IEnumerable<X> {
    return (T)items.Where(x => !x.Hidden && !x.Parent.Hidden);
}

This works except that I have other types than X (say Y and Z) that I also want to write LimitVisible functions for. But I can’t write

public static T LimitVisible<T>(this T items) where T : IEnumerable<Y> {
    return (T)items.Where(y => !y.Hidden && !y.Parent.Hidden);
}

because you can overload based on generics. I could put these methods in different classes, but that doesn’t seem like the right solution.

Any suggestions? Maybe I’m making incorrect assumptions and there’s no need for an IQueryable specific version in the first place.

Edit: Another Option

Here’s another pattern I’ve used in the past to avoid duplication

private static readonly Expression<Func<X, bool>> F = x => !x.Hidden && !x.Parent.Hidden;

public static IEnumerable<X> LimitVisible(this IEnumerable<X> e) {
    return e.Select(W.Compile());
}

public static IQueryable<X> LimitVisible(this IQueryable<X> q) {
    return q.Select(W);
}

Are there any dangers with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:08:18+00:00Added an answer on June 13, 2026 at 3:08 pm

    The answer depends on your usage: if you do not mind bringing the data set into memory in order to perform the query, then keeping only the IEnumerable<T> override will be fine.

    However, the inevitable consequence of this decision is that your queries will be forced into memory the moment that you use LimitVisible<T>. This may or may not be what you want, and might force you into coding patterns that you would rather avoid. For example,

    var firstHundred = ctx
        .SalesOrders
        .LimitVisible()                     // Happens in memory
        .Where(ord => ord.UserId == myUser) // Also happens in memory
        .Take(100);
    

    will perform a lot worse with a single IEnumerable<T> than with an IQueryable<T> overide availalbe, because all sales orders will be retrieved into memory one-by-one before determining their visibility, rather than checking the condition on the server side. If filtering by user ID could eliminate the majority of sales orders on the server side, the performance difference of this equivalent query could be orders of magnitude better:

    var firstHundred = ctx
        .SalesOrders
        .Where(ord => ord.UserId == myUser) // Happens in RDBMS
        .LimitVisible()                     // Happens in memory
        .Take(100);
    

    If you plan to use your code only by yourself and you do not mind being watchful for situations when there is bad performance for no obvious reason, you can keep one overload. If you plan to let others use your library, I strongly suggest keeping both overrides.

    EDIT : To speed up your alternative implementation, pre-compile your expression, like this:

    private static readonly Expression<Func<X, bool>> F = x => !x.Hidden && !x.Parent.Hidden;
    private static readonly Predicate<X> CompiledF = (Predicate<X>)F.Compile();
    
    public static IEnumerable<X> LimitVisible(this IEnumerable<X> e) {
        return e.Select(CompiledF);
    }
    
    public static IQueryable<X> LimitVisible(this IQueryable<X> q) {
        return q.Select(F);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
This could be a duplicate question, but I have no idea what search terms
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.