Built with EF I have a class Product

[Table("Products")]
public class Product
{
    public Product()
    {
        this.LanguageProduct = new List<LanguageProduct>();
    }
    [Key, ScaffoldColumn(false)]
    public int ProductId { get; set; }
    public virtual ICollection<LanguageProduct> LanguageProduct { get; set; }
}

which is linked by a one-to-many relationship to another class that records the name in an unlimited number of languages:

public class LanguageProduct
{
    [Key, Column(Order = 0), ForeignKey("Product")]
    public int ProductID { get; set; }
    [Key, Column(Order = 1), ForeignKey("Languages"), StringLength(6)]
    public string Language { get; set; }

    [StringLength(100), Display(Name = "Product name")]
    public string ProductName { get; set; }

    public virtual LanguagesList Languages { get; set; }
}

If you haven't noticed the class LanguageProduct has a composite key based on two foreign keys. Everything works fine until now. In one of my aspx page I try to populate a dropdownlist order by the product name in one language.

public IQueryable GetProductsList()
    {
        var _db = new ApplicationDbContext();
        IQueryable query = _db.Products.OrderBy(p => p.LanguageProduct.Where(z => z.Language == "en-US").SelectMany(x => x.ProductName));
        return query;
    }

As you might suspect, my issue comes from that query that returns nothing. I've tried anything without succes. Among other things I have added the MultipleActiveResultSets to my connection string which I don't like. Have you got any indication to solve this?

Related posts

Recent Viewed