1

I'm getting an exception when the GetProducts() method is called below. I'm essentially trying to filter down my list of Products by what is available in the specified Country. There is a one to many relationship setup between a Product and a Country.

    public static List<Product> GetProducts(Country country)
    {
        Context db = new Context();
        return db.Products.Where(m => m.Countries.Contains(country)).ToList<Product>();
    }

Unable to create a constant value of type 'DataModels.Country'. Only primitive types or enumeration types are supported in this context.

If I'm not going about this the right way, what is the best way to filter Products by the single selected Country?

4

1 に答える 1

3

プリミティブ型に基づいてのみ比較できます。

メソッドを使用するように変更しますAnyID国エンティティキー(または一意のプロパティ)に置き換えます

db.Products.Where(m => m.Countries.Any(c => c.ID == country.ID)).ToList<Product>();
于 2012-10-10T17:44:59.720 に答える