Lambda表達式怎麼寫SQL中的in?

ambda表達式查詢沒有IN這個方法,能夠變通一下,in查詢的數組是否包含在映射對象裏面的集合裏數組

直接貼代碼吧,一看就懂了spa

class Program
    {
        static void Main(string[] args)
        {
            List<Model> strList = new List<Model>()
            {
                new Model(){Id=1,Name = "張三"},
                new Model(){Id=2,Name = "李四"},
                new Model(){Id=3,Name = "王五"},
                new Model(){Id=4,Name = "趙六"},
            };

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

            var list = strList.Where(x => whereList.Contains(x.Id));

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

            Console.ReadKey();
        }
    }

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

運行結果:code

相關文章
相關標籤/搜索