LINQ query to find if items in a list are contained in another list

前端 未结 9 481
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 00:14

I have the following code:

List test1 = new List { \"@bob.com\", \"@tom.com\" };
List test2 = new List

        
9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 00:45

    Try the following:

    List test1 = new List { "@bob.com", "@tom.com" };
    List test2 = new List { "joe@bob.com", "test@sam.com" };
    var output = from goodEmails in test2
                where !(from email in test2
                    from domain in test1
                    where email.EndsWith(domain)
                    select email).Contains(goodEmails)
                select goodEmails;
    

    This works with the test set provided (and looks correct).

提交回复
热议问题