I need to write a function called IsExists(string TableName,string KeyColumnName,string ValueToCheck)
in DAL which checks whether the data exists in the particular table and in the particular column which I am passing
Basically I want to achieve something like this when I try to put up in sql query
select count(id) from "+TableName+" where "+keyColumnName+"="+ValueToCheck+";
But I cant use sql query ..
In my solution I have an .edmx-file, an entity class along with a repository class, which has SearchFor
method:
public class EntityRepository<C, TEntity> : IEntityRepository<TEntity>
where TEntity : class
where C : DbContext
{
public IQueryable<TEntity> SearchFor(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate)
{
return _entities.Set<TEntity>().Where(predicate);
}
}
I Tried something like this
public bool CheckIsExists<T>(string keyColumnName, string valueToCheck) where T : class
{
bool isExist = false;
using (var repository = ContextFactory.CreateEmployeeMasterRepository())
{
var repo = repository.GetEntityRepository<T>();
object obj = (T)Activator.CreateInstance(type);
repo.SearchFor(u => u.GetType().GetProperty(keyColumnName).GetValue(obj).ToString() == valueToCheck);
}
return isExist;
}
Which is again not working ..
Somebody help me in doing this. i have been trying from long time.. suggestions are greatly appreciated.