从列表组中选择2个或更多部门的公共元素,然后计数

时间:2013-06-04 14:04:16

标签: c# linq .net-3.5

我有一个看起来像这样的课程:

{
    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int CategoryId { get; set; }
        public int DepId{ get; set; }
    }
}

我的列表折叠的数据如下:

ID  |  Name     |  CategoryId  |  DepId
---------------------------------------
1   |  Post     |  1           |  1
2   |  Post     |  1           |  2
3   |  Printer  |  2           |  1
4   |  Printer  |  2           |  3
5   |  Post     |  3           |  3
6   |  Printer  |  2           |  1

此数据将Access数据保存到某些类别。

我想得到的是两个(或更多)部门的常见类别

如果用户选择他想要id = 1的部门的类别,那么他应该获得id为1和3的元素,但如果他想要部门1和3的类别,他应该得到元素4和6。

对于DepId IN(1,3)我想得到这个结果:

Name     |  CategoryId
----------------------
Printer  |  2

像SQL中的JOIN。

我能够在sql中编写代码:

SELECT * FROM(
SELECT  
    C.Cat_Id AS Id,
    MAX(C.Name) AS Name,
FROM 
    Categories_Access AS CA (NOLOCK)
    JOIN dbo.Categories AS C (NOLOCK) ON C.Cat_Id = CA.Cat_Id
WHERE 
    CA.DepId IN (1,3)
    GROUP BY C.Cat_Id
    HAVING COUNT(*)=2
) A ORDER BY A.Name

现在我想在C#中做同样的事情。

修改 这是我的尝试:

var cat = new List<Category>();
            cat.Add(new Category {Id = 1, CategoryId = 1, Name = "Post", DepId = 1});
            cat.Add(new Category {Id = 2, CategoryId = 1, Name = "Post", DepId = 2});
            cat.Add(new Category {Id = 3, CategoryId = 2, Name = "Printer", DepId = 1});
            cat.Add(new Category {Id = 4, CategoryId = 2, Name = "Printer", DepId = 3});
            cat.Add(new Category {Id = 5, CategoryId = 3, Name = "Another", DepId = 3});
            cat.Add(new Category {Id = 6, CategoryId = 2, Name = "Printer", DepId = 1});
            cat.Add(new Category {Id = 7, CategoryId = 4, Name = "Else", DepId = 1});

            var ids = new List<int> {1, 2};

            var Query = from p in cat.Where(i => ids.Contains(i.DepId)).GroupBy(p => p.CategoryId)
                        select new
                        {
                            count = p.Count(),
                            p.First().Name,
                            p.First().CategoryId
                        };

enter image description here

我需要做的只是选择count = ids.Count。

的项目

我的结局版本(基于@roughnex答案):

private static IEnumerable<Cat> Filter(IEnumerable<Category> items, List<int> ids)
{
    return items.Where(d => ids.Contains(d.DepId))
   .GroupBy(g => new { g.CategoryId, g.Name })
   .Where(g => g.Count() == ids.Count)
   .Select(g => new Cat { Id = g.Key.CategoryId, Name = g.Key.Name });
}

2 个答案:

答案 0 :(得分:2)

在C#(LINQ)中选择你将使用的常用元素

List<int> Depts = new List<int>() {1, 3};


var result = Categories.Where(d => Depts.Contains(d.DeptId))
                       .GroupBy(g => new {g.CatId, g.Name})
                       .Where(g => g.Count() >= 2)
                       .Select(g => new {g.Key.CatId, g.Key.Name});

答案 1 :(得分:0)

因此,基于您所说的内容,您已经将SQL proc中的数据解析为List<Category>。如果是这种情况,以下代码段应该指导您:

var items = new List<Category>();

var deptIds = new List<int>() { 1, 3 };

var query = items.Where(item => deptIds.Contains(item.DepId))
            .Select(category => category);

如果要在LINQ中执行IN,则必须将其反转并使用包含。而在SQL中它是ColumnName IN(List),而在LINQ中它是List.Contains(ColumnName)。希望有所帮助。