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

2022/3/19 19:30:31

本文主要是介绍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();
        }

  



这篇关于C# 使用Linq 实现类似SQL中IN的用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程