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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:21:28+00:00 2026-05-28T14:21:28+00:00

This method works great and generates only 1 query (utilizing .Includes() to eager load).

  • 0

This method works great and generates only 1 query (utilizing .Includes() to eager load).
However, I would like to refactor the AbleToDelete code to be more reusable.
(Using EF4)

        public override IEnumerable<AffectProjection> SelectAll(SearchAffects page)
        {
            IQueryable<Affect> query = BuildSearchQuery(page);

            IEnumerable<AffectProjection> results = query.Select(entity => new AffectProjection()
            {
                AffectID = entity.AffectID,
                AffectCode = entity.AffectCode,
                AffectName = entity.AffectName,

                AbleToDelete = !((entity.Foo1s.Any(pa => !pa.Inactive))
                                || (entity.Foo2s.Any(ea => !ea.Inactive))
                                || (entity.Foo3s.Any(sa => !sa.Inactive))
                                || (entity.Foo4s.Any(spa => !spa.Inactive)))
            }).ToArray();

            return results;
        }

I moved the code into an Expression Func structure but can’t figure out how to replace my code that is setting the AbleToDelete property. Advise?

public Expression<Func<Affect, bool>> DelegateExpression = entity =>
            !((entity.Foo1s.Any(pa => !pa.Inactive))
                || (entity.Foo2s.Any(ea => !ea.Inactive))
                || (entity.Foo3s.Any(sa => !sa.Inactive))
                || (entity.Foo4s.Any(spa => !spa.Inactive)));

Last but not least, this solution compiles but fails at runtime with error: “NotSupportedException – The LINQ expression node type ‘Invoke’ is not supported in LINQ to Entities.” Trying to use a delegate which isn’t supported:

        public override IEnumerable<AffectProjection> SelectAll(SearchAffects page)
        {
            IQueryable<Affect> query = BuildSearchQuery(page);

            //Create delegate instance and register method -- single statement
            var deleteAbilityAllowed = new CanDelete(GetAbleToDelete);

            IEnumerable<AffectProjection> results = query.Select(entity => new AffectProjection()
            {
                AffectID = entity.AffectID,
                AffectCode = entity.AffectCode,
                AffectName = entity.AffectName,

                AbleToDelete = deleteAbilityAllowed(entity)
            }).ToArray();

            return results;
        }

        public delegate bool CanDelete(Affect entity);

        public bool GetAbleToDelete(Affect entity)
        {
            return !((entity.Foo1s.Any(pa => !pa.Inactive))
                || (entity.Foo2s.Any(ea => !ea.Inactive))
                || (entity.Foo3s.Any(sa => !sa.Inactive))
                || (entity.Foo4s.Any(spa => !spa.Inactive)));
        }

Please advise and thank you in advance!

  • 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-28T14:21:28+00:00Added an answer on May 28, 2026 at 2:21 pm

    LINQKit contains methods that allow you to do this. With it, you can use yout DelegateExpression. To do that, add AsExpandable() to the source of your query and then invoke the Expression using Invoke():

    var expression = DelegateExpression;
    
    var results = query.AsExpandable().Select(entity => new AffectProjection()
    {
        AffectID = entity.AffectID,
        AffectCode = entity.AffectCode,
        AffectName = entity.AffectName,
        AbleToDelete = expression.Invoke(entity)
    }).ToArray();
    

    Be careful though, it seems you can’t use the expression directly from the field, it has to be from a local variable.

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

Sidebar

Related Questions

In my view controller's -viewDidLoad method, I call [myTextField becomeFirstResponder]; This works like a
Why does only the third method work? $('#jqtest').live('load', function() {$(this).html('hi');}); //1 $('#jqtest').load(function() {$(this).html('hi');}); //2
This method works as expected - it creates a JTree with a root node
I am having trouble constructing a jqGrid TreeGrid using local data. This method works
I can't figure out why this is. I have a connect method that works
I'm trying to seed my database with the standard db/seeds.rb method. This works fine
actually i use this method to show similar words for a search request.. $query
How can i make an extension method that will work like this public static
I know downcasting like this won't work. I need a method that WILL work.
Can this method work? public String sayHello(){ return Hello.jsp?name= + laala; } I am

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.