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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:13:03+00:00 2026-05-14T00:13:03+00:00

Suppose I have an entity object defined as public partial class Article { public

  • 0

Suppose I have an entity object defined as

public partial class Article  
{  
    public Id
    {
        get;
        set;
    }  
    public Text
    {
        get;
        set;
    }  
    public UserId
    {
        get;
        set;
    }  
}

Based on some properties of an Article, I need to determine if the article can be deleted by a given user. So I add a static method to do the checking. Something like:

public partial class Article  
{  
    public static Expression<Func<Article, bool>> CanBeDeletedBy(int userId)
    {  
        //Add logic to be reused here
        return a => a.UserId == userId;
    }  
}

So now I can do

using(MyEntities e = new MyEntities())  
{
    //get the current user id
    int currentUserId = 0;

    e.Articles.Where(Article.CanBeDeletedBy(currentUserid));  
}

So far so good. Now I want to reuse the logic in CanBeDeletedBy while doing a Select, something like:

using(MyEntities e = new MyEntities())  
{
    //get the current user id
    int currentUserId = 0;

    e.Articles.Select(a => new  
    {  
        Text = a.Text,  
        CanBeDeleted = ???  
    };  
}

But no matter what I try, I can’t use the expression in the select method. I guess that If I can do

    e.Articles.Select(a => new  
    {  
        Text = a.Text,  
        CanBeDeleted = a => a.UserId == userId
    };  

Then I should be able to use the same expression. Tried to compile the expression and call it by doing

    e.Articles.Select(a => new  
    {  
        Text = a.Text,  
        CanBeDeleted = Article.CanBeDeletedBy(currentUserId).Compile()(a)
    }; 

but it won’t work either.

Any ideas on how to get this to work? Or if it isn’t possible, what are the alternatives to reuse business logic in both places?

Thanks

Pedro

  • 1 1 Answer
  • 1 View
  • 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-14T00:13:04+00:00Added an answer on May 14, 2026 at 12:13 am

    Re-using expression trees is a black art; you can do it, but you would need to switch a lot of code to reflection and you’d lose all the static checking. In particular, working with the anonymous types becomes a nightmare (although dynamic in 4.0 might be workable).

    Further, if you cheat and use Expression.Invoke, then it isn’t supported by all providers (most noticeably not on EF in .NET 3.5SP1).

    Unless this is a major pain point, I’d leave it with duplication. Or do you need to re-use the expression tree?

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

Sidebar

Related Questions

Suppose I have public IList<Entity> Children { get; set; } public NotifyChildren(Func<object, bool> action,
Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
Suppose I have the following 2 entities: @Entity public class Person implements Serializable {
Technology: Hibernate 3.0 Suppose i have Entity class Company @Entity @Table(name=tbl_companies) public class Company
I'm trying out Entity Framework Code first CTP4. Suppose I have: public class Parent
Suppose we have typical repository public class Repository:IRepository<Entity> { public Entity GetById(int id) {
Suppose I have a static method of my class that returns an object of
Suppose I have an Employee object with the following properties: string Name { get;
Suppose you have a method: public void Save(Entity data) { this.repositoryIocInstance.EntitySave(data); } Would you
and have it map to a strongly typed object? Suppose I have a entity

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.