简体   繁体   English

LINQ分组和忽略列

[英]LINQ grouping and ignoring column

I'm using EF and LINQ. 我正在使用EF和LINQ。 I have the following in my db table 我的数据库表中有以下内容

branchId      Name        ItemId          CategoryId
2             Test1       1               1
5             Test1       1               1
3             Test1       1               2
2             Test2       2               1
7             Test2       2               1

I need to group by ItemId and BranchId should be ignored, so the output should be 我需要按ItemId分组,应该忽略BranchId,因此输出应为

Name  ItemId  CategoryId
Test1 1       1
Test1 1       2
Test2 2       2

Please help. 请帮忙。 thanks 谢谢

You need to apply group by which is on multiple column, so for that you need to go for code like as below which do group by on multiple columns.... 您需要在多个列上应用分组依据,因此,您需要执行如下代码,在多个列上进行分组...。

var numberGroups = from it in context.items
                   group it by new { it.ItemId,it.Name,it.CateogryID } into g
                   select new { ItemID = g.Key.ItemID, Name= g.Key.Name
                                CategoryID = g.Key.CategoryID };
var query = from item in context.Item
            group by item.Name,ItemId,item.CategoryId
           select item.Name,Item.Id,item.CategoryId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM