【发布时间】:2014-01-05 16:49:19
【问题描述】:
我想在 LINQ 中使用 IN 运算符。我有一个如下所示的 SQL 查询。我如何包含 where 条件。
SQL 查询
SELECT ServiceId, ServiceName, Rate
FROM Service
WHERE (ServiceId IN (1, 2, 3, 4))
LINQ
string serviceId = "(1,2,3,4,5,6)";
try
{
using (var context = new DBEntities())
{
var query = (from c in context.Service
where c.ServiceId == serviceIds //ServiceId is the primary key
select new
{
serviceId = c.ServiceId,
serviceName = c.ServiceName,
rate = c.Rate
}).ToList();
GridView1.DataSource = query.ToList();
GridView1.DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
【问题讨论】:
-
使用集合和
.Contains()方法。