Click here to Skip to main content
16,016,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all
I am using asp.net 4.5 with code first using the repository pattern in c#.

base class :

C#
public interface IAuditableEntity
        {
            ...
            string DeletedBy { get; set; }
            DateTime? DeletedDate { get; set; }
            bool IsDeleted { get; set; }
        }
    public abstract class AuditableEntity:  IAuditableEntity
            {
                ...
                public string DeletedBy { get; set; }
                public DateTime? DeletedDate { get; set; }
                public bool IsDeleted { get;set; }
            }


class:
C#
public class Group : AuditableEntity
        {
            public int Id {get;set;}
            public string Code { get; set; }
            public string Name { get; set; }
            public GroupType Type { get; set; }
            public string Description { get; set; }
            public int? ParentId { get; set; }
    
        }


Repository Base :

C#
public class RepositoryBase<T> : IRepository<T> where T : class
        {
            protected DbContext _entities;
            protected DbSet<T> dbSet;
            #endregion
            public RepositoryBase(DbContext context)
            {
                _entities = context;
                dbSet = context.Set<T>();
            }
            ...
            public virtual IQueryable<T> GetAll()
            {
                try
                {
                   
                    return dbSet;
                      
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public virtual IQueryable<T> GetMany(Expression<Func<T, bool>> where)
            {
                return dbSet.Where(where);
            }
           ...
    
    
        }


My Question:
i want to add expression in `GetAll` and `GetMany` function if Repositorybase T use `IAuditableEntry`

What I have tried:

C#
public virtual IQueryable<T> GetAll()
                {
                    try
                    {
                       
                        return dbSet.Where(x=> x is IAuditableEntity && !((IAuditableEntity)x).IsDeleted);
                          
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }


When i used this
return dbSet.Where(x=> x is IAuditableEntity && !((IAuditableEntity)x).IsDeleted);

it show error like :

The 'TypeIs' expression with an input of type
'Group' and a check of type
'Base.IAuditableEntity' is not supported. Only entity
 types and complex types are supported in LINQ to Entities queries.
Posted
Updated 22-Feb-20 23:02pm
Comments
Richard Deeming 25-Feb-20 10:34am    
catch (Exception ex)
{
    throw ex;
}

Don't do that! You've just thrown away the stack trace of the exception, making it much harder to work out where it was thrown from.

If you really want to re-throw an exception, just use throw; instead of throw ex;:
catch (Exception)
{
    throw;
}


But in this case, since you're not doing anything other than rethrowing the exception, there's no point catching it in the first place. Just remove the try..catch block, and let the exception propagate normally.
Richard Deeming 25-Feb-20 10:36am    
If you're using Entity Framework Core, you might want to set up a global filter to hide soft-deleted entities:
Global Query Filters - EF Core | Microsoft Docs[^]

1 solution

You can test for T instead:
C#
public virtual IQueryable<T> GetAll()
{
   try
   {
      return (T is IAuditableEntity)
         ? dbSet.Where(x => !((IAuditableEntity)x).IsDeleted)
         : dbSet.All();
   }
   catch (Exception ex)
   {
      // Here you should do something with the exception (log it for example, or display its details).
      // If you rethrow it, it will be as if you never caught it in the first place.
   }
}
 
Share this answer
 
v2
Comments
Maciej Los 23-Feb-20 12:06pm    
5ed!
BTW: do not forget to add x=> into Where statement.
phil.o 23-Feb-20 12:19pm    
Corrected. Thanks Maciej :)

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



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