How to pass "Philippines,China,Canada" as parameter @list to IN clause? This isn't working: SELECT * FROM Country WHERE CountryName IN (@list)
Using Linq, that problem can be easily solved. There's no method on scalar type that can test against a list, a good Linq provider (used Linq-to-NHibernate here) don't have a problem translating this(list against scalar) though:
var countryList = from c in s.Query<Country>() where new[]{ "Philippines", "China" }.Contains(c.CountryName) select c;
The generated query:
exec sp_executesql N'select country0_.CountryId as CountryId1_, country0_.CountryName as CountryN2_1_, country0_.Population as Population1_ from [Country] country0_ where country0_.CountryName in (@p0 , @p1)',N'@p0 nvarchar(4000),@p1 nvarchar(4000)',@p0=N'Philippines',@p1=N'China'
Linq can accept variable list too:
Console.Write("Input list: "); string[] list = Console.ReadLine().Split(','); // example input is Philippines,Canada,China var countryList = from c in s.Query<Country>() where list.Contains(c.CountryName) select c;
The generated query:
exec sp_executesql N'select country0_.CountryId as CountryId1_, country0_.CountryName as CountryN2_1_, country0_.Population as Population1_ from [Country] country0_ where country0_.CountryName in (@p0 , @p1 , @p2)',N'@p0 nvarchar(4000),@p1 nvarchar(4000),@p2 nvarchar(4000)',@p0=N'Philippines',@p1=N'Canada',@p2=N'China'
No comments:
Post a Comment