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

The Archive Base Latest Questions

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

Not sure if what I am trying is possible or not, but I’d like

  • 0

Not sure if what I am trying is possible or not, but I’d like to reuse a linq expression on an objects parent property.

With the given classes:

class Parent {
 int Id { get; set; }
 IList<Child> Children { get; set; }
 string Name { get; set; }
}
class Child{
 int Id { get; set; }
 Parent Dad { get; set; }
 string Name { get; set; }
}

If i then have a helper

Expression<Func<Parent,bool> ParentQuery() { 
  Expression<Func<Parent,bool> q = p => p.Name=="foo";
}

I then want to use this when querying data out for a child, along the lines of:

using(var context=new Entities.Context) {
 var data=context.Child.Where(c => c.Name=="bar" 
 && c.Dad.Where(ParentQuery));
}

I know I can do that on child collections:

using(var context=new Entities.Context) {
 var data=context.Parent.Where(p => p.Name=="foo" 
 && p.Childen.Where(childQuery));
}

but cant see any way to do this on a property that isnt a collection.
This is just a simplified example, actually the ParentQuery will be more complex and I want to avoid having this repeated in multiple places as rather than just having 2 layers I’ll have closer to 5 or 6, but all of them will need to reference the parent query to ensure security.

If this isnt possible, my other thought was to somehow translate the ParentQuery expression to be of the given type so effectively:
p => p.Name==”foo”;
turns into:
c => c.Dad.Name==”foo”;
but using generics / some other form of query builder that allows this to retain the parent query and then just have to build a translator per child object that substitutes in the property route to the parent.

EDIT:
Following on from comments by @David morton

Initially that looks like I can just change from Expression to a delegate function and then call
.Where(ParentQuery()(c.Dad));

However I am using this in a wider repository pattern and cant see how I can use this with generics and predicate builders – I dont want to retrieve rows from the store and filter on the client (web server in this case). I have a generic get data method that takes in a base expression query. I then want to test to see if the supplied type implements ISecuredEntity and if it does append the securityQuery for the entity we are dealing with.

public static IList<T> GetData<T >(Expression<Func<T, bool>> query) {
 IList<T> data=null;
 var secQuery=RepositoryHelperers.GetScurityQuery<T>();
 if(secQuery!=null) {
  query.And(secQuery);
 }
 using(var context=new Entities.Context()) {
  var d=context.GetGenericEntitySet<T>();
  data=d.ToList();
 }
 return data;
}

ISecuredEntity:

public interface ISecuredEntity : IEntityBase {
    Expression<Func<T, bool>> SecurityQuery<T>();
}

Example Entity:

public partial class ExampleEntity:  ISecuredEntity {
    public Expression<Func<T, bool>> SecurityQuery<T>() {
        //get specific type expression and make generic
        Type genType = typeof(Func<,>).MakeGenericType(typeof(ExampleEntity), typeof(bool));
       var q = this.SecurityQuery(user);
        return (Expression<Func<T, bool>>)Expression.Lambda(genType, q.Body, q.Parameters);         
    }

     public Expression<Func<ExampleEntity, bool>> SecurityQuery() {
        return e => e.OwnerId==currentUser.Id;

    }
}

and repositoryHelpers:

internal  static partial class RepositoryHelpers {
    internal static Expression<Func<T, bool>> SecureQuery<T>() where T : new() {
        var instanceOfT = new T();
        if (typeof(Entities.ISecuredEntity).IsAssignableFrom(typeof(T))) {
            return ((Entities.ISecuredEntity)instanceOfT).SecurityQuery<T>();
        }
        return null;
    }
}

EDIT Here is the (eventual) solution

I ended up going back to using expressions, and using LinqKit Invoke. Note: for EF I also had to call .AsExpandable() on the entitySet

The key part is being able to call:

 Product.SecureFunction(user).Invoke(pd.ParentProduct);

so that I can pass in the context into my parent query

My end classes look like:

public interface ISecureEntity {
 Func<T,bool> SecureFunction<T>(UserAccount user);
}


public class Product : ISecureEntity {
 public Expression<Func<T,bool>> SecureFunction<T>(UserAccount user) {
  return SecureFunction(user) as Expression<Func<T,bool>>; 
 }
 public static Expression<Func<Product,bool>> SecureFunction(UserAccount user) {
  return f => f.OwnerId==user.AccountId;
 }
 public string Name { get;set; }
 public string OwnerId { get;set; }
}


public class ProductDetail : ISecureEntity {
 public Expression<Func<T,bool>> SecureFunction<T>(UserAccount user) {
  return SecureFunction(user) as Expression<Func<T,bool>>; 
 }
 public static Func<ProductDetail,bool> SecureFunction(UserAccount user) {
  return pd => Product.SecureFunction(user).Invoke(pd.ParentProduct);
 }
 public int DetailId { get;set; }
 public string DetailText { get;set; }
 public Product ParentProduct { get;set; }
}

Usage:

public IList<T> GetData<T>() {
 IList<T> data=null;
 Expression<Func<T,bool>> query=GetSecurityQuery<T>();
 using(var context=new Context()) {
  var d=context.GetGenericEntitySet<T>().Where(query);
  data=d.ToList();
 }
 return data;
}
private Expression<Func<T,bool>> GetSecurityQuery<T>() where T : new() {
  var instanceOfT = new T();
        if (typeof(Entities.ISecuredEntity).IsAssignableFrom(typeof(T))) {
            return ((Entities.ISecuredEntity)instanceOfT).SecurityQuery<T>(GetCurrentUser());
        }
        return a => true; //returning a dummy query
    }
}

Thanks for the help all.

  • 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-14T00:42:53+00:00Added an answer on May 14, 2026 at 12:42 am

    You’re overthinking it.

    First, don’t return an Expression<Func<Parent, bool>>, that’ll require you to compile the expression. Return simply a Func<Parent, bool> instead.

    Next, it’s all in how you call it:

     context.Children.Where(c => c.Name == "bar" && ParentQuery()(c.Dad));
    
     context.Parents.Where(ParentQuery());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure if this is possible, but here is what I am trying to
I am not sure if this is even possible but I am trying to
I'm not sure if what I'm even trying to do is possible but here
I'm not sure if this is possible in Spring MVC 3.0, but I'm trying
Not sure if this is even possible but I am trying to save text
im not sure if this is possible or not, but im trying to alter
Not sure if it's possible but I've been trying to use curl to essentially
I'm not sure this is possible but I'm trying to declare an Object that
not sure if this is possible keeping the code simple, but im trying to
I'm not sure if it's possible but I'm trying to do an insert while

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.