How do I use the SQL WHERE IN construct with PetaPoco?

前端 未结 5 1137
鱼传尺愫
鱼传尺愫 2020-12-24 13:20

I have a database table named Tags (Id, Name) from which I would like to select the ones where the name matches a name in a list. In SQL I would use something like:

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 13:40

    Here is an another sample:

    program.cs:

    public static void Main(string[] args)
    {
        using (var db = new PetaPoco.Database("Northwind"))
        {
            var sql = "Select * from customers where Country in (@Countries)";
            var countries = new { @Countries = new string[] { "USA", "Mexico" } };
            var customers = db.Query(sql, countries);
            foreach (var customer in customers)
            {
                Console.WriteLine("{0} - {1} from {2}", customer.CustomerID, customer.CompanyName, customer.Country);
            }
        }
    }
    

    customer.cs:

    public class Customer
    {
        public string CustomerID { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
        public string Country { get; set; }
        public string Fax { get; set; }
        public string Phone { get; set; }
        public string PostalCode { get; set; }
        public string Region { get; set; }
    }
    

    App.config: (Connectionstring uses localdb connectionstring, so you might change it.)

    
    
         
            
        
      
        
        
      
    
    

提交回复
热议问题