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

  • Home
  • SEARCH
  • 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 243559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:55:53+00:00 2026-05-11T20:55:53+00:00

Ref to: Creating a ValidationAttribute to ensure unique column values. Ok… Let’s try reframing

  • 0

Ref to:Creating a ValidationAttribute to ensure unique column values.

Ok… Let’s try reframing the question:

from here I have ripped this code:

    static TEntity Get<TEntity, TKey>(this DataContext ctx, TKey key) where TEntity : class
    {
        var table = ctx.GetTable<TEntity>();
        var pkProp = (from member in ctx.Mapping.GetMetaType(typeof(TEntity)).DataMembers
                      where member.IsPrimaryKey
                      select member.Member).Single();
        ParameterExpression param = Expression.Parameter(typeof(TEntity), "x");
        MemberExpression memberExp;
        switch (pkProp.MemberType)
        {
            case MemberTypes.Field: memberExp = Expression.Field(param, (FieldInfo)pkProp); break;
            case MemberTypes.Property: memberExp = Expression.Property(param, (PropertyInfo)pkProp); break;
            default: throw new NotSupportedException("Invalid primary key member: " + pkProp.Name);
        }
        Expression body = Expression.Equal(
        memberExp, Expression.Constant(key, typeof(TKey)));
        var predicate = Expression.Lambda<Func<TEntity, bool>>(body, param);
        return table.Single(predicate);
    }

It’s a DataContext Extension Method that has stuff I need. However, it requires generic parms I don’t have available – attributes don’t allow generic subcalsses, so….

I have hacked the code to this point:

    public override bool IsValid(object value)
        SomeDataContext context = SomeDataContext.GetNewDataContext();
        var table = context.GetTable(_EntityType);
        var codeProp = (from member in context.Mapping.GetMetaType(_EntityType).DataMembers
                        where member.Name == _PropertyName
                        select member.Member).Single();
        ParameterExpression param = Expression.Parameter(_EntityType, "x");
        MemberExpression memberExp = Expression.Property(param, (PropertyInfo)codeProp); 
        Expression body = Expression.Equal(memberExp, Expression.Constant(value, typeof(char)));
        Type lambdaType = typeof(Func<,>).MakeGenericType(_EntityType, typeof(bool)); 
        var predicate = Expression.Lambda(lambdaType, body, param); 
        object code = table.FirstOrDefault(predicate);
        if (code != null)
        {
            return false;
        }
        return true;
    }

This line is bad:

        object code = table.FirstOrDefault(predicate);

as I stated here the answers to which don’t help.

  • var enumerator =
    ((IEnumerable)table).GetEnumerator();object
    code = enumerator.MoveNext() ?
    enumerator.Value : null;

    is not useful in my attempt to build the expression tree.
  • IEnumerable<object> tableGeneric =
    ((IEnumerable)table).OfType<object>();

    has issues when I try:
    object code = tableGeneric.FirstOrDefault(predicate);

So, I am pretty stuck. Any ideas out there?

Thanks.

  • 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-11T20:55:53+00:00Added an answer on May 11, 2026 at 8:55 pm

    The end-run you may be looking for is that as far as I can tell you really want a method like:

    public bool IsValid<TEntity>()
    {
        // validation logic goes here
    }
    

    So that you can perform the check(s) that you need, but you need to implement it in a method that only has the run-time-type available to it:

    public bool IsValid(Type entityType)
    {
        // ???
    }
    

    If that is the case, then you implement the first code-block with the logic you need, and you implement the second method as:

    public bool IsValidWrapper(Type entityType)
    {
        // You may want to be stricter than this, but the essence is to
        // get a handle to the generic method first...
        MethodInfo genMethod = GetType().GetMethod("IsValid");
    
        // Bind it to the type you want to operate on...
        MethodInfo method = genMethod.MakeGenericMethod(entityType);
    
        // And invoke it...
        return (bool) method.Invoke(this, null);
    }
    

    It won’t win any beauty contests or performance crowns, but it will get you what you need. And note that if performance is a concern, you could keep a lookup from Type to a delegate with the signature bool delegate() so that you do not have to use slow reflection for a given type more than once to perform the type binding.

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

Sidebar

Ask A Question

Stats

  • Questions 217k
  • Answers 217k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See Creating Project and Item Templates on MSDN. May 12, 2026 at 11:24 pm
  • Editorial Team
    Editorial Team added an answer I have two approaches to this, both of which have… May 12, 2026 at 11:24 pm
  • Editorial Team
    Editorial Team added an answer You'll want a unique Facade interface of your design, bridging… May 12, 2026 at 11:24 pm

Related Questions

Ref to: Creating a ValidationAttribute to ensure unique column values. Ok... Let's try reframing
I'm creating a class that inherits from a parent class with protected instantiation. The
I am having some troubles passing a reference to an object which is of
I'm creating a function where I need to pass an object so that it
I am creating a Windows Forms control derived from UserControl to be embedded in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.