1. 程式人生 > >Lambda表示式怎麼寫SQL中的in?

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

ambda表示式查詢沒有IN這個方法,可以變通一下,in查詢的陣列是否包含在對映物件裡面的集合裡

直接貼程式碼吧,一看就懂了

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; } }

執行結果: