Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
58 views
in Technique[技术] by (71.8m points)

How to implement Entity Framework genetic property match for query?

I have a method like this:

    protected async Task<bool> IsExist(Category entity)
    {
        return await _context.Cateogries.AnyAsync(s => s.Integrated == entity.Integrated);
    }

I have many entities that have a property Integrated. I want to create a generic method to match for all types.

    protected async Task<bool> IsExist(Func<TEntity, bool> property, bool value)
    {
        return await _context.Set<TEntity> AnyAsync(s => property(s) == value);
    }

And using it like this:

   IsExist(s => s.Integrated, true)

But it does not work:

InvalidOperationException: The LINQ expression 'DbSet() .Any(y => Invoke(__property_0, y) .Value == __value_1)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

question from:https://stackoverflow.com/questions/65848922/how-to-implement-entity-framework-genetic-property-match-for-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...