【问题标题】:Get count from join table - EFCodeFirst从连接表中获取计数 - EFCodeFirst
【发布时间】:2011-04-17 16:29:46
【问题描述】:

所以我最近开始检查 ASP.NET MVC3 和 EFCodeFirst,但遇到了问题。 我有 2 个模型: 人:

    [Bind(Exclude = "PersonID")]
    public class Person {

        public int PersonID { get; set; }

        [Required(ErrorMessage="Given name is a required field")]
        [DisplayName("Given name")]
        public string GivenName { get; set; }

        [Required(ErrorMessage="Family name is a required field")]
        [DisplayName("family name")]
        public string FamilyName { get; set; }

        [Required(ErrorMessage = "Birthdate is a required field")]
        [DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime Birthdate { get; set; }

        [DisplayName("Ingredient")]
        public virtual Ingredient Ingredient { get; set; }
}

和成分:

public class Ingredient {
    public int IngredientID { get; set; }

    [Required(ErrorMessage = "Ingredient is a required field")]
    public string Name { get; set; }

    public virtual ICollection<Person> Persons { get; set; }
}

现在的重点是成分是一个半固定的表(只有业务用户可以添加成分,但这是另一回事),一个人可以输入他/她的数据和他/她最喜欢的成分。 这一切都很好,但我想通过加入 2 个表来创建前 10 种成分,然后执行分组、排序、计数和限制(例如到 10 个)。

这里的问题是我已经尝试了几件事,但无法让它发挥作用。我对 LINQ 还很陌生,不知道该怎么做。

任何建议将不胜感激!

编辑: 要再次指定,我只需要 LINQ 中的以下查询:

SELECT i.name, COUNT(p.Ingredient_IngredientID) AS Total
FROM People p, Ingredients i
WHERE p.Ingredient_IngredientID = i.IngredientID
GROUP BY i.name
ORDER BY total DESC;

【问题讨论】:

    标签: c# .net asp.net linq asp.net-mvc-3


    【解决方案1】:

    您应该将public Ingredient IngredientID { get; set; } 标记为虚拟,这样成分表就会延迟加载。此外,您应该将其称为成分,因为它不仅仅是 ID,它是被引用的实际成分。

    如果我理解正确,您想知道哪种成分最受欢迎?如果是这样,只需按 Ingredient.IngredientID 对人员进行分组,然后计算每个分组中的人数。

    另一种做法是添加一个

    public virtual ICollection&lt;Person&gt; Persons { get; set;} 字段添加到成分类,现在您可以这样做了

    var tenMostPopularIngredients = (from i in Ingredients
                                orderby i.Persons.Count() descending
                                select i).Take(10);
    

    这为您提供了一个字典,其中每个成分名称作为键,计数作为值,按计数降序排列。如果你得到一个 ArgumentException,那是因为你有多个同名的成分:

    var tenMostPopularIngredients = (from i in ingredients
                                    let count = i.Persons.Count()
                                    orderby count descending
                                    select new 
                                    {
                                       Ingredient = i, 
                                       Count = count
                                     }
                                     ).Take(10).ToDictionary(t => t.Ingredient.Name,t => t.Count);
    

    【讨论】:

    • +1 我没有注意到他没有指定从成分到人的导航。
    • 说得太早了,显然它有点有效,但只是给了我一份包含重复的每种成分的列表。即: - 番茄 - 沙拉 - 苹果 - 番茄
    • 你确定你没有重复的成分?试试我刚刚添加到答案中的内容。
    • 你说得对,我有重复的成分。不知道如何解决,所以我提出了一个新问题link
    【解决方案2】:

    OrderByTake 的组合还不够?

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多