0

I am using the Microsoft Entity Framework. Consider the following example code:

public class person
{
    public int Id { get; set;}
    public string Name { get; set;}
    public string DisplayName{ get; set;}

    public string OtherStuff{get; set;}

}

public MyContext : DbContext
{
    public class DbSet<person> persons {get; set;} 
}

// In the main...
using(MyContext context = new MyContext)
{
    var results = context.persons
                .Where(p => p.Name == "someName")
                .FirstOrDefault();
}

I would like to do some processing prior to the query actually running based on the arguments to the Where Clause. I would like access to the Property the comparator and the argument if possible.

Essentially, I would like to override the were clause such that I could do some pre-processing and then complete the query and return the results. Is there a mechanism to do this? I would like this to be specific to this specific DBContext and transparent to the caller.

For example, I would like to create a hash of the Name property, and then use it to find the person record in the database.

As another example, I would like to log each name that has been looked up, once this operation is done just return the data.

Thanks

6
  • 2
    "some pre-processing" - Give some examples of what you want to do, currently we can't give you any help. Commented Oct 15, 2024 at 19:44
  • @PoulBak I updated my question with a couple of examples. Commented Oct 15, 2024 at 20:02
  • Looks like you do not understand how EF works. It generates SQL from LINQ query. If you think, that some "hashes" will speedup your query, you are wrong. Better explain root of the problem. Commented Oct 15, 2024 at 21:25
  • Did someone take it upon themselves to remove the very helpful answers that sere posted here? Incidentally I know exactly how Entity Framework works. I am not trying to speed up anything. The root of the problem is I want access to the predicate information in a LINQ call before the query is generated and sent to the database. I find it unfortunate that the answers that were provided were taken down without explanation. Commented Oct 16, 2024 at 10:11
  • I think what you want might require overloading DbContext to make sure that when you call context.Table, you'd get a custom type that overrides .Where(). As long as said type inherits DbSet<T> you might be able to make it work, but I don't think it's simple. Commented Oct 16, 2024 at 12:01

1 Answer 1

0

I am not sure what you mean but I get the feeling you want "someName" to not be clear text but a hash. And then compare it to the hashed value in the database. If that is the case I say it is not possible. Not by knowing how EF works, but knowing how SQL and RDB works.

I guess you want a linq query like

var results = context.persons
                .Where(p => Hash(p.Name) == "abcd")
                .FirstOrDefault();

If that had been possible it would result in an SQL like:
select * from persons where Hash(Name) == "abcd"
which means that the RDB must do a full table scan and do the hash for every record and then do the comparison. While, possibly, technically possible, it is a bad solution (I won't delve into why).

A more correct solution would be to update the database table with a hash of the name.

If I miss your question - please feedback and I will remove this answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.