"Include" lambda in generic repository

c# entity-framework-6 lambda

Question

I follow repository pattern and I have the following method in my generic repository class:

    public virtual T Get(Expression<Func<T, bool>> where)
    {
        return dbset.Where(where).FirstOrDefault<T>();
    }

I would like to add the a lambda expression for including navigation properties. Is it possible?

1
0
10/27/2014 2:43:31 PM

Popular Answer

There's my generic repo Get method:

    public virtual IQueryable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> Query = DbSet;

        if (filter != null)
        {
            Query = Query.Where(filter);
        }

        if (!string.IsNullOrEmpty(includeProperties))
        {
            foreach (string IncludeProperty in includeProperties.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries))
            {
                Query = Query.Include(IncludeProperty);
            }
        }

        if (orderBy != null)
        {
            return orderBy(Query);
        }
        return Query;
    }
0
10/27/2014 3:42:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow