C# 使用Linq 实现类似SQL中IN的用法


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

 private static List<Model> LinqIn()
        {
            List<Model> strList = new List<Model>()
            {
                new Model(){Id=1,Name = "张三"},
                new Model(){Id=11,Name = "张三1"},
                new Model(){Id=2,Name = "李四"},
                new Model(){Id=3,Name = "王五"},
                new Model(){Id=30,Name = "王五"},
                new Model(){Id=4,Name = "赵六"},
            };

            List<int> whereList = new List<int>() { 1, 3 };

            List<Model> list = strList.Where(x => whereList.Contains(x.Id)).ToList();

            foreach (var model in list)
            {
                Console.WriteLine("Id:" + model.Id + "Name:" + model.Name);
            }
            Console.WriteLine("list count=="+list.Count);
            return list;
        }

//测试
  static void Main(string[] args)
        {
            List<Model> list = LinqIn();

            Console.ReadLine();
        }

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM