LINQ中实现从集合中查找在指定子集合中存在的记录

如有集合:List<user> 它有属性userid,username,它的值是

userid=1

username=“zzl”

userid=2

username=”lr”

userid=3

username=”zhz”

现在有要求,找到userid为1和3的user集合,怎么找?

已知得到用户集合名为userList

看我是这样实现的,如果大家有其它实现的方法,欢迎留言!

  class User
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<User> userList = new List<User> 
            {
               new User{UserID=1,UserName="zzl"},
               new User{UserID=2,UserName="lr"},
               new User{UserID=3,UserName="zhz"},
            };
            int[] userids = { 1, 3 };
            userList.Where(i => userids.Contains(i.UserID));
        }
    }
原文地址:https://www.cnblogs.com/lori/p/2111826.html