我正在寻找一些帮助,使用C#/ LINQ设计查询以满足以下要求:
我有一系列公司: -
Id Name Email Address
1 Company A a@a.com abc
2 Company B b@b.com abc
3 Company C c@c.com abc
4 Company D d@d.com abc
5 Company A a@a.com abc
我的目标是根据两个字段检测重复的项目,在此示例中为“name”和“email”。
所需的输出是以下所示的客户列表:
所需的重复列表: -
Id Qty Name Email Address
1 2 Company A a@a.com abc (Id/details of first)
2 1 Company B b@b.com abc
3 1 Company C c@c.com abc
4 1 Company D d@d.com abc
答案 0 :(得分:9)
如果您明确要在每组重复项中使用最低ID记录,则可以使用
var duplicates = companies
.GroupBy(c => new { c.Name, c.Email })
.Select(g => new { Qty = g.Count(), First = g.OrderBy(c => c.Id).First() } )
.Select(p => new
{
Id = p.First.Id,
Qty = p.Qty,
Name = p.First.Name,
Email = p.First.Email,
Address = p.First.Address
});
如果您不关心使用哪个记录的值,或者您的来源已按ID(升序)排序,则可以放弃OrderBy
来电。
答案 1 :(得分:4)
from c in companies
group c by new { c.Name, c.Email } into g
select new
{
Id = g.First().Id,
Qty = g.Count(),
Name = g.Key.Name,
Email = g.Key.Email,
Address = g.First().Address
};