1

我有一个存储库方法,例如,

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

multipletownship 参数值是1,2。这些是乡镇 id。问题是我如何将 townshipid 过滤为多个。if condition这可能或者我必须处理一些循环......

4

1 回答 1

2

如果,在 multipletownship 字符串中没有,那么您将得到一个包含单个项目的数组:

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
}
于 2013-02-06T08:52:37.280 回答