简体   繁体   中英

Use Include and Where in Find Method in Asp.Net Core

I have This Code in My Controllers.

   var project = await _context.Projects
            .Include(p => p.Customer)
            .SingleOrDefaultAsync(m => m.Project_Id == id);

But I Want use in generic repository that i write this method Generic Repository

       public async Task<T> GetByIdIncludes(Expression<Func<T, bool>> predicate, string includes = "")
        {
            return await _dbContext.Set<T>().Where(predicate).Include(includes).FirstOrDefaultAsync();  
        }

and in my controller Replace with this code MyController

 var project = await _genericRepository.GetByIdIncludes(F => F.Customer_Id == id, "Customer");

but the FindByIdInclude Method in the generic repository was returned null to the controller, why?

Put include before where in your sample

public async Task<T> GetByIdIncludes(Expression<Func<T, bool>> predicate, string includes = "")
        {
            return await _dbContext.Set<T>().Include(includes).Where(predicate).FirstOrDefaultAsync();  
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM