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 3321642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:05:15+00:00 2026-05-17T23:05:15+00:00

I’m trying to DRY out some lambda expressions for security rights. Is it possible

  • 0

I’m trying to DRY out some lambda expressions for security rights. Is it possible to take a lamda expression and apply it to a single entity for true?

Like lets say I have a Person and a DocumentFolder

    Expression<Func<Person, bool>> CanSeePerson()
    {
        return c =>  !c.IsPrivate;
    }

And one for the folder

    Expression<Func<DocumentFolder, bool>> CanSeeFolder()
    {
        return c => !c.IsPrivate && c.Owner.CanSeePerson(); // <- ???
    }

How the heck can I use that CanSeePerson() function on a single type to return true and maintain an expression that can be used in linq queries like such

Entities.DocumentFolder.Where(CanSeeFolder());

I know how to use the where on an iqueryable but I can’t see how to apply the expression tree to a single value.


This throws the error: Unable to create a constant value of type ‘Person’. Only primitive types (‘such as Int32, String, and Guid’) are supported in this context.

Expression<Func<DocumentFolder, bool>> CanSeeFolder()
    {
        return c => !c.IsPrivate &&_entities.Persons.Where(x => x.Id == c.Owner.Id).Any(CanSeePerson());
}

The difference seems to be based on putting the IQueryable Directly in the statement. This also does NOT work

Expression<Func<DocumentFolder, bool>> CanSeeFolder()
    {
        return c => !c.IsPrivate &&_entities.Persons.Where(CanSeePerson()).Contains(c.User);
    }

but this DOES work

Expression<Func<DocumentFolder, bool>> CanSeeFolder()
        {
            var canSeePersons = _entities.Persons.Where(CanSeePerson());
            return c => !c.IsPrivate && canSeePersons.Contains(c.User);
        }

p.s. I know I suck @ using this stackoverflow formatting thing lol


The reason why this works is because the CanSeePerson() function cannot be converted and used in an expression. When you put the canSeePersons variable in the function instead you are placing in an iQueryable type which can be used in an expression. Using “var” convolutes it a bit.

  • 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-05-17T23:05:15+00:00Added an answer on May 17, 2026 at 11:05 pm

    You defined a Expression<Func<Person, bool>> that solves your problem. The trick is to simply transform your list of objects to the list you want to filter. It is not always possible, but many times it is. You just have to be a bit creative 🙂

    When using extension methods you can easily come up with a solution that allows you to do this:

    var visibleFolders = Entities.DocumentFolder.WhereCanSeeFolder();
    

    Here is the (completely DRY) solution:

    public static class SecurityExtensions
    {
        public static IQueryable<DocumentFolder> WhereCanSeeFolder(
            this IQueryable<DocumentFolder> folders)
        {
            var visibleOwners = folders.Select(f => f.Owner)
                .Where(CanSeePerson);
    
            return
                from folder in folders.Where(CanSeeFolder)
                where visibleOwners.Contains(folder.Owner)
                select folder;
        }
    
        private static readonly Expression<Func<DocumentFolder, bool>>
            CanSeeFolder = folder => !folder.IsPrivate;
    
        private static readonly Expression<Func<Person, bool>>
            CanSeePerson = person => !person.IsPrivate;
    }
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

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.