0

How can I use a "left outer join" in this Function? If we use "query.Include" then we use an "inner join" but I need a "left outer join". What is your plan for this problem?

        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();
    }
Ken White
  • 123,280
  • 14
  • 225
  • 444
Arman
  • 47
  • 2
  • 8
  • https://stackoverflow.com/questions/51410209/linq-method-syntax-with-inner-and-outer-join, https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/method-based-query-syntax-examples-join-linq-to-dataset – qqtf Jul 05 '21 at 03:12

1 Answers1

0

i fix it check this

 /// <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();
        }

how to use:

        _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 );
     
   
Arman
  • 47
  • 2
  • 8