【发布时间】:2018-12-29 04:56:39
【问题描述】:
我正在尝试在 SQL 中执行以下操作
SELECT COUNT(*) AS "Total"
FROM dbo.Items
WHERE CategoryID IN (SELECT CategoryID
FROM Categories
WHERE Name = 'Beverages')
任何想法我们如何在LINQ 中实现这一点?
*更新
Item类代码:
public class Item
{
[Key]
public int ItemID { get; set; }
public virtual Category Category { get; set; }
public virtual Brand Brand { get; set; }
public int CategoryID { get; set; }
public int BrandID { get; set; }
[Display(Name ="Product Name")]
[Required(ErrorMessage = "Product name is required")]
public string ItemName { get; set; }
[Display(Name="Product Price")]
public decimal? ItemPrice { get; set; }
[DataType(DataType.ImageUrl)]
[Display(Name = "Image URL")]
public string ImageUrl { get; set; }
}
Category类代码:
public class Category
{
public int CategoryID { get; set; }
[DisplayName("Category Name")]
public virtual string Name { get; set; }
public virtual List<Item> Items { get; set; }
}
【问题讨论】:
-
分类表的主键是什么
-
CategoryId 的类型应该是 int 不能为空的 int?
-
您认为这可能是导致问题的原因吗?
-
是的,试着把它改成int
-
对不起兄弟,同样的错误信息:
LINQ to Entities does not recognize the method 'OnlineStore.Models.Category get_Item(Int32)' method, and this method cannot be translated into a store expression.
标签: linq