Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Problem

How to check id exist or not generic repository pattern asp.net core 2.1 ?

because i have more than 10 controllers so that i need to write function check

if id on table exist or not .

like that
C#
if (!EmployeeExists(emp.EmployeeId))
                    {
                        return NotFound();
                    }


What I have tried:

I try that by this not work

C#
bool CheckIfEntityExistsByEntityId(Expression<Func<T, bool>> expr);
public bool CheckIfEntityExistsByEntityId<T>(Expression<Func<T, bool>> expr)
        {
            return dbSet.Any(u => expr);
        }

I have compile error on expr
Posted
Comments
Richard MacCutchan 8-Feb-19 11:18am    
"I have compile error on expr"
You have been told enough times already: if you get an error message then you need to tell us what it is; we cannot guess.
Richard Deeming 12-Feb-19 13:11pm    
At the risk of contradicting the other Richard:
public bool CheckIfEntityExistsByEntityId<T>(Expression<Func<T, bool>> expr)

Why have you re-declared the generic type parameter on the method? You should be using the type parameter from the class instead.
public bool CheckIfEntityExistsByEntityId(Expression<Func<T, bool>> expr)
Member 14825024 13-Jun-20 12:09pm    
It still doesn't work
Error:
Cannot implicity convert type System.Linq.Expression<system.func<object, bool="">> to bool

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900