1

db model schema

I have this model in my DB, and specific classes to represent it in ef core.

I am using GenericRepository and trying to get a collection of DietProduct table which later would be adapted by auto mapper for a view.

The problem starts when I try to get the list of all active DietProducts with DietproductLocalization where LanguageId = @LanguageId;

I tried to complete a generic method like this:

public List<TEntity> FindComplex(Expression<Func<TEntity, bool>> predicate, params string[] includes)
        {
            IQueryable<TEntity> query = Context.Set<TEntity>();

            foreach (var include in includes)
                query = query.Include(include);

            return query.Where(predicate).ToList();
        }

and then use the select as:

int currentLanguage = LanguageUtility.GetLanguageId(CultureInfo.CurrentCulture);
var dp = _repo.DietProduct.FindComplex(x => x.Active 
                                            && x.DietProductLocalization
                                                 .Any(l => l.LanguageId == currentLanguage), "DietProductLocalization");

but no luck so far, because it keeps returning all the rows from included table, of course as include stands for all or nothing.

Is there any way to write the generic method and specific query so I don't have to use specific queries with inner joins all over?

Ermir Beqiraj
  • 867
  • 11
  • 24
  • 1
    If you are using Auto Mapper you should take a look at the [QueryableExtensions](https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions) helper methods it has built in. It is designed to make your mappings work on IQueryable objects without pulling everything in to memory or needing to do manual joins like you are. – Scott Chamberlain Apr 15 '17 at 19:30
  • 1
    Please see [this](http://stackoverflow.com/questions/16798796/ef-include-with-where-clause) answer as you may find it helpful. – CodingYoshi Apr 15 '17 at 19:47
  • @ScottChamberlain Thanks for the response, but I was really hoping to get some ef-related solution – Ermir Beqiraj Apr 15 '17 at 20:07
  • @CodingYoshi thanks for the response, yes I have seen that post, the problem with it is because I can't turn it into a generic. – Ermir Beqiraj Apr 15 '17 at 20:08
  • EF itself has no solution as it is, so it means you'll have to filter in memory or resort to extension frameworks - you'd need to intersept EF query generating if you want to do it yourself DB-side, which is not a good idea IMO. – DevilSuichiro Apr 15 '17 at 21:52
  • 1
    It's very simple. EF never gratified this often-requested feature. So now at least once a week there's a question along these lines at Stack Overflow. – Gert Arnold Apr 16 '17 at 08:38
  • @DevilSuichiro and GertArnold thanks for your effort anyway, It seems like we have to type the select queries manually, all of them :) The link I provided to my question on how to solve one case of these was since 2009, I was just thinking/hoping that someone solved it somehow. Cheers to everyone – Ermir Beqiraj Apr 16 '17 at 10:38

0 Answers0