【问题标题】:How to use a left outer join in a generic repository?如何在通用存储库中使用左外连接?
【发布时间】:2021-09-15 16:16:59
【问题描述】:

如何在此函数中使用“左外连接”? 如果我们使用“query.Include”,那么我们使用“内连接”,但我需要一个“左外连接”。 您对这个问题有什么计划?

        public virtual IEnumerable<TEntity> GetData(Expression<Func<TEntity, bool>> where = null
        , Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null
        , List<string> includes = null)//List<Expression<Func<TEntity, IProperty>>>
    {
        IQueryable<TEntity> query = _dbset;
        if (where != null)
        {
            query = query.Where(where);
        }

        if (orderBy != null)
        {
            query = orderBy(query);
        }
        if (includes != null)
        {
            foreach (var includesItem in includes)
            {
                query = query.Include(includesItem);
            }
        }

        return query.ToList();
    }

【问题讨论】:

标签: linq generics entity-framework-core repository


【解决方案1】:

我修复它检查这个

 /// <summary>
        /// 
        /// </summary>
        /// <param name="whereData"></param>
        /// <param name="orderBy">
        /// how To Use :
        /// GetData(where, orderBy: q => q.OrderByDescending(d => d.Date), null)
        /// </param>
        /// <param name="includes">
        /// how To Use :
        /// new List<string> { "SerialList", "CategoryItem" }
        /// </param>
        /// <param name="leftJoinSelection">
        /// زمانی که ارتباط یک به یک داریم و نیاز داریم ایتم هایی با
        /// حتما با OR از هم جدا بشه
        /// how To Use :
        /// You Must Use Or  on Many Left Join Parameter , Not Use And!
        ///  x=>x.CategoryID==0  | x.PersonID == 0 
        /// </param>
        /// <returns></returns>IEnumerable<TSource> source, Func<TSource, TKey>
        public virtual IEnumerable<TEntity> GetData(Expression<Func<TEntity, bool>> whereData = null
            , Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
             List<string> includes = null, Expression<System.Func<TEntity, bool>> leftJoinSelection = null)
        //List<Expression<Func<TEntity, IProperty>>>
        //  , List<string> includes = null
        {
            IQueryable<TEntity> query = _dbset;
            IQueryable<TEntity> leftData = _dbset;
            if (whereData != null)
            {
                query = query.Where(whereData);
            }


            if (includes != null)
            {
                foreach (var includesItem in includes)
                {
                    query = query.Include(includesItem);
                }

            }
            if (leftJoinSelection != null)//we use left join mode
            {
                if (whereData != null)
                {
                    leftData = leftData.Where(ArmanHelper.Helper.And(whereData, leftJoinSelection));
                }
                else
                {
                    leftData = leftData.Where(leftJoinSelection);
                }
            }
            else //لفت جوین نداره پس مرتب سازی فراموش نشه
            {//dont use left join mode then normal orderBy
                if (orderBy != null)
                {
                    query = orderBy(query);
                }
            }

            List<TEntity> returnData = new List<TEntity>();
           
            if (leftJoinSelection != null)//we use left join mode
            {
                returnData = query.ToList();
                returnData.AddRange(leftData.ToList());
                if (orderBy != null)
                {
                    if (orderBy != null)
                    {
                       returnData = orderBy(returnData.AsQueryable()).ToList();
                    }
                }
            }
            else // افت جوین نداریم و روال عادی داده ها لیست میشه و برمیگرده
            {//dont use Left join Mode then return normal method
                return query.ToList();
            }

            return returnData.ToList();
        }

使用方法:

        _genericRepositoryTrade = new GenericRepository<Commodity>(_eFContextA);
        _genericRepositoryPerson = new GenericRepository<CommoditySerial>(_eFContextA);
        _genericRepositoryCategory = new GenericRepository<Category>(_eFContextA);

  var trades1 = _genericRepositoryTrade.GetData(x => x.ID < 1000, orderBy: q => q.OrderByDescending(d => d.ID)
                , new List<string> { "SerialList", "CategoryItem" },
                x=>x.CategoryID==0 );
     
   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 2020-06-11
    • 2015-03-06
    • 2011-08-17
    • 2018-09-05
    • 1970-01-01
    • 2022-01-20
    • 2015-04-13
    相关资源
    最近更新 更多