0

I have a product, this product can have some descriptions one by language.

I'd like based on the product.Reference and the Language.Code get the product and the description matching the language code. I use EF Core 2.0

I can do it with 2 separated query but I'd like one if it's possible.

I tried this :

var product = _context.Products
    .Where(x => x.Reference == "3265709")
    .Include(x => x.ProductDescriptions)
    .ThenInclude(x => x.Where(lg => lg.Language.Code == "EN").Select(z => z.Language))
    .ToList();

Any idea ?

Thanks,

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Reference { get; set; }
    public ICollection<ProductDescription> ProductDescriptions{ get; set; }
}

public class ProductDescription
{
    public int Id { get; set; }
    public string Short { get; set; }
    public string Complete { get; set; }
    public Language Language{ get; set; }
    public Product Product { get; set; }
}

public class Language
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}
4
  • 2
    Can you post the two queries you used? Commented Aug 17, 2017 at 13:47
  • I see how do it in two queries but didn't do yet. I'd like in one query first. Commented Aug 17, 2017 at 13:50
  • 1
    Your question isn't crystal clear (to me at least). If you show the two-query solution it might help others with understanding what it is exactly that you need. Commented Aug 17, 2017 at 13:52
  • not crystal clear ? just the Linq query to get product (and one description matching the language) based on the product reference and the language of the description. Commented Aug 17, 2017 at 13:54

1 Answer 1

1

Could you do something like this? You might not even need the .Any() part.

var product = _context.Products.Where(x => x.Reference == "3265709" && x.ProductDescriptions.Any(a => a.Language.Code == "EN"))
   .SelectMany(x => x.ProductDescriptions.Where(z => z.Language.Code == "EN").Select(a => a.Language)).ToList();

EDIT:

Is this what you are looking for then? This should give you a list of products with the Reference code specified and a filtered Product Description list based on the language code.

var product = _context.Products.Where(x => x.Reference == "3265709" && x.ProductDescriptions.Any(a => a.Language.Code == "EN")).Select(x => new Product {
      Id = x.Id,
      Name = x.Name,
      ProductDescriptions = x.ProductDescriptions.Where(a => a.Language.Code == "EN").ToList(),
      Reference = x.Reference
});
Sign up to request clarification or add additional context in comments.

5 Comments

The result is a language not a Product with the ProductDescription but there are some ideas to take
Oh I thought based on this part of your linq .Select(z => z.Language) that you wanted to return a list of Languages.
I put in some new code, hope this is what you were looking for
Why not but recreate the product object I lose the other 'include' I have
I thought you were asking for products matching the product.Reference and had a product description that had a language code EN only. Doesnt my code provide this? Why are you using Includes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.