分组使用Linq

时间:2014-03-23 00:37:47

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

我需要提供按类别分组的广告列表,包括每个类别的广告计数。

类别按照包括“轿车”,“敞篷跑车”和“运动”等类别的汽车等父类别进行分组。

型号:

public class Ad
{
    [Key]
    public int Id { get; set; }

    public Category Category { get; set; }
}

public class Category
{
    public int Id { get; set; }

    [ForeignKey("CategoryParent")]
    public int? CategoryParent_Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Ad> Ads { get; set; }
}

结果是:

 Cars - Count: 100 (where 100 is the sum of for example 20 Saloon's Ads, 80 Cabrilet's)

目前,我只能提供所有类别的列表,而不是按父类别分组。

var adIds = {1,2,4,5}

var result = 
    from c in categoryQuery
    let searchCount = c.Ads.Count(a => adIds.Contains(a.Id))
    where searchCount > 0
    select new CategoryGetAllBySearchDto
    {
        Id = c.CategoryParent_Id,
        Name = c.CategoryParent.Name,
        SearchCount = searchCount,
        Ids = c.Ads.Where(a => adIds.Contains(a.Id)).Select(a => a.Id)
    }; 

3 个答案:

答案 0 :(得分:1)

我认为你需要这个:

var adIds = { 1, 2, 4, 5 };
var result = from c in categoryQuery
             where c.Ads.Any(a => adIds.Contains(a.Id))
             group c by new {c.CategoryParent_Id, c.CategoryParent.Name} into g
             select new CategoryGetAllBySearchDto
             {
                 Id = g.Key.CategoryParent_Id,
                 Name = g.Key.Name,

                 SearchCount = g.SelectMany(u => u.Ads)
                                .Where(a => adIds.Contains(a.Id))
                                .Count(),

                 Ids = g.SelectMany(u => u.Ads)
                        .Where(a => adIds.Contains(a.Id))
                        .Select(a => a.Id)
             };

答案 1 :(得分:1)

您可以SearchCount将[{1}}添加到AsEnumerable以获取查询一次

Ids

和查询:

    public class CategoryGetAllBySearchDto
        {
            public int? Id { get; set; }
            public string Name { get; set; }

            public int SearchCount { get {  return this.Ids.Count() } }

            public IEnumerable<int> Ids { get; set; }
        }

答案 2 :(得分:1)

内存中的

GroupBy

var adIds = { 1, 2, 4, 5 };
var result = categoryQuery.Where(c => c.Ads.Any(a => adIds.Contains(a.Id)))
                        .Select(c => new 
                        { 
                            c.CategoryParent_Id, 
                            c.CategoryParent.Name,
                            Ids = c.Ads.Where(a => adIds.Contains(a.Id)).Select(a => a.Id).AsEnumerable()
                        })
                        .ToList()
                        .GroupBy(c => new {c.CategoryParent_Id, c.Name})
                        .Select(g => new CategoryGetAllBySearchDto
                         {
                             Id = g.Key.CategoryParent_Id,
                             Name = g.Key.Name,
                             Ids = g.SelectMany(u => u.Ids).AsEnumerable()
                         })
                         .ToList();