我有一个类似的存储库方法
public List<Township> lst(string multipletownship)
{
var foo = from x in db.Townships
select new { x.TownshipID, x.JTownshipName };
string[] sarr;
if (multipletownship.Contains(','))
{
sarr = multipletownship.Split(',');
foo = foo.Where(s => s.TownshipID == sarr[0] || s.TownshipID==sarr[1]);
}
else
{
foo = foo.Where(s => s.TownshipID == multipletownship);
}
multipletown参数值是1,2
。这些是town id。问题是我如何在if condition
将townshipid过滤为多个。这是可能的,或者我必须处理一些循环。。。
如果multipletown字符串中没有,
,那么您将获得一个包含单个项的数组:
public List<Township> lst(string multipletownship)
{
string[] sarr = multipletownship.Split(',');
var foo = from x in db.Townships
where sarr.Contains(x.TownshipID)
select new { x.TownshipID, x.JTownshipName };
// return
}