【问题标题】:Count total based on a foreign key value using LINQ使用 LINQ 根据外键值计算总数
【发布时间】: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


【解决方案1】:

更新 型号

public class Item
{
    [Key]
    public int ItemID { get; set; }
    public Category Category { 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; }

}

public class Category
{
    [Key]
    public int CategoryID { get; set; }
    public virtual string Name { get; set; }
    public virtual List<Item> Items { get; set; }
}

使用的数据样本

您想要商品的类别 = 饮料 你可以从items结束

var itemsCount1 = dbcontext
    .Items
    .Count(x => x.Category.Name == "Beverages");

你可以从categories结束

//如果没有名称为Beverages的Category,则返回0

var itemsCount2 = dbcontext
    .Categories
    .FirstOrDefault(x => x.Name == "Beverages")?.Items?.Count() ?? 0;

查询结果

【讨论】:

  • CS1061 'Items' 不包含 'Categories' 的定义,并且找不到接受“Items”类型的第一个参数的扩展方法 'Categories'(按 F4 添加 using 指令或程序集参考)
  • 在您的类别模型中,您应该有导航属性 List
  • 我尝试了你给出的第一个代码示例......即来自items
  • 对于您给出的第二个示例 - 错误消息: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.
  • 你能添加类别,项目模型类
【解决方案2】:

您可以通过关注获得计数。

var results = Categories.Where(x=>x.Name.Equals("Beverages")
                    .Join(Items,
                    c=>c.CategoryID,
                    i=>i.CategoryID,
                    (c,i)=> i).Count();

对于模拟数据,

var Categories = new List<Category>
{
    new Category{CategoryID =1, Name ="Beverages"},
    new Category{CategoryID =2, Name ="One"},
    new Category{CategoryID =3, Name ="Two"}
};

var Items = new List<Item>
{
    new Item{ItemID=3,CategoryID=4,ItemName="abc1"},
    new Item{ItemID=1,CategoryID=1,ItemName="abc2"},
    new Item{ItemID=1,CategoryID=1,ItemName="abc3"},
    new Item{ItemID=1,CategoryID=1,ItemName="abc4"},
    new Item{ItemID=1,CategoryID=1,ItemName="abc5"},
    new Item{ItemID=2,CategoryID=1,ItemName="abc6"},
    new Item{ItemID=3,CategoryID=4,ItemName="abc7"},
    new Item{ItemID=4,CategoryID=2,ItemName="abc8"},
};

输出:5

更新

var results = dbContext.Categories.Where(x=>x.Name.Equals("Beverages")
                        .Join(dbContext.Items,
                        c=>c.CategoryID,
                        i=>i.CategoryID,
                        (c,i)=> i).Count();

【讨论】:

  • 在 Visual Studio 中,您的查询对关键字 .Join 表示错误:The name 'Join' does not exist in the current context.'
  • 您与 Join 一起使用的对象类型是什么?说如果它的类别。加入,类别的类型是什么?希望它是一个集合?
  • 我相信您正在使用基于其他答案和 OP 中的一些 cmets 的 EF。我已经更新了答案。请检查
  • 很抱歉,您的更新没有运行。 .Join 关键字也有同样的错误。
  • 您能展示一下您尝试过的内容吗?特别是,您正在使用什么数据类型/集合
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 2012-02-28
相关资源
最近更新 更多