C# 查找重复项并将其从列表中删除的Linq查询

C# 查找重复项并将其从列表中删除的Linq查询,c#,linq,C#,Linq,我正在尝试创建一个LINQ查询,它将在两个列表中查找重复项,并将它们从第一个列表中删除 下面的代码将找到重复项并返回它们,但我希望查询从notificationsFirst返回唯一项: using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args)

我正在尝试创建一个LINQ查询,它将在两个列表中查找重复项,并将它们从第一个列表中删除

下面的代码将找到重复项并返回它们,但我希望查询从notificationsFirst返回唯一项:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            InnerJoinExample();
            Console.ReadLine();
        }

        class Notification
        {
            public string Name { get; set; }
            public int Id { get; set; }
        }

        public static void InnerJoinExample()
        {
            Notification first = new Notification { Name = "First", Id = 1 };
            Notification second = new Notification { Name = "Second", Id = 2 };
            Notification third = new Notification { Name = "Third", Id = 3 }; 
            Notification fourth = new Notification { Name = "Fourth", Id = 4 };
            Notification fifth = new Notification { Name = "Fifth", Id = 5 };

            List<Notification> notificationsFirst = new List<Notification> { first, second, third };
            List<Notification> notificationsSecond = new List<Notification> { third, fourth, fifth };

            var query = from notiFirst in notificationsFirst
                        join notiSecond in notificationsSecond on notiFirst.Id equals notiSecond.Id
                        select new Notification { Name = notiFirst.Name, Id = notiFirst.Id };


            foreach (var not in query)
            {
                Console.WriteLine($"\"{not.Name}\" with Id {not.Id}");
            }
        }

        // This code should produce the following:
        //
        // "First" with Id 1
        // "Second" with Id 2
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
InnerJoinExample();
Console.ReadLine();
}
类通知
{
公共字符串名称{get;set;}
公共int Id{get;set;}
}
公共静态void InnerJoinExample()
{
通知优先=新通知{Name=“first”,Id=1};
通知秒=新通知{Name=“second”,Id=2};
第三个通知=新通知{Name=“third”,Id=3};
第四个通知=新通知{Name=“fourth”,Id=4};
第五次通知=新通知{Name=“fifth”,Id=5};
列表通知sfirst=新列表{first,second,third};
列表通知SECOND=新列表{第三、第四、第五};
var query=来自notificationsFirst中的notiFirst
在notificationsSecond on NotifiFirst.Id中加入notiSecond等于notiSecond.Id
选择新通知{Name=notiFirst.Name,Id=notiFirst.Id};
foreach(变量不在查询中)
{
Console.WriteLine($“\{not.Name}\”,Id为{not.Id});
}
}
//此代码应生成以下内容:
//
//Id为1的“第一个”
//Id为2的“第二个”
}
}

您应该将
方法与
Intersect
结合使用

其思想是使用
Intersect
找出给定初始列表的交集列表,然后只使用
从第一个集合中除去该列表


您应该结合使用
Intersect
使用
Except
方法

其思想是使用
Intersect
找出给定初始列表的交集列表,然后只使用
从第一个集合中除去该列表


??谢谢你!工作起来很有魅力。在真正的代码中,notificationSecond实际上是一个DBContext,我正试图对数据库异步执行此操作
var query=notificationsFirst.Except(notificationsFirst.Intersect(wait_context.Notifications))但它抱怨DbSet没有GetAwaiter@Q-bertsuit,您应该使用
wait\u context.Notifications.toListSync()
。完美!非常感谢!谢谢你!工作起来很有魅力。在真正的代码中,notificationSecond实际上是一个DBContext,我正试图对数据库异步执行此操作
var query=notificationsFirst.Except(notificationsFirst.Intersect(wait_context.Notifications))但它抱怨DbSet没有GetAwaiter@Q-bertsuit,您应该使用
wait\u context.Notifications.toListSync()
。完美!非常感谢!
var query = notificationsFirst.Except(notificationsFirst.Intersect(notificationsSecond));