LINQ查询获取在数据库表中出现两次的项目?

时间:2017-11-27 17:40:41

标签: c# entity-framework linq

您好我有一个表艺术家,并且在该表中有值BandID,我想从Artist列表中获取在Artist表中出现两次的BandID。什么是正确的LINQ查询?

public class Artiest
    {
        public int ArtiestID { get; set; }
        public string Naam { get; set; } 

        public int InstrumentID { get; set; }
        public Instrument Instrument { get; set; }

        public int PopgroepID { get; set; }
        public Popgroep Popgroep { get; set; }

    }

我试图将其转换为LINQ,但我无法翻译它。

 var q =
        from g in arr.GroupBy(x => x)
        where g.Count() == 1
        select g.First();

1 个答案:

答案 0 :(得分:0)

您应该指定您的问题。究竟什么是“重复价值”给你?决定哪个值?在这些重复的艺术家中,你想要哪一个PopGroepId? 一种可能的解决方案是

context.Artiests
    .GroupBy(x=>x.Naam)
    .Where(x=>x.Count()==2)
    .Select(x=>x.FirstOrDefault().PopGroepId);

这将获得第一个Artiest的PopGroepId,其名称恰好是数据库表中的两个条目。

相关问题