我的问题是这个问题的一部分:

我从一个表单中接收id的集合。我需要获取键,将它们转换为整数,并从DB中选择匹配的记录。

[HttpPost]
public ActionResult Report(FormCollection collection)
{
    var listofIDs = collection.AllKeys.ToList();  
    // List<string> to List<int>
    List<Dinner> dinners = new List<Dinner>();
    dinners= repository.GetDinners(listofIDs);
    return View(dinners);
}

当前回答

使用Linq:

var intList = stringList.Select(s => Convert.ToInt32(s)).ToList()

其他回答

我认为这是最简单的方法:

var listOfStrings = (new [] { "4", "5", "6" }).ToList();
var listOfInts = listOfStrings.Select<string, int>(q => Convert.ToInt32(q));

使用Linq:

var intList = stringList.Select(s => Convert.ToInt32(s)).ToList()

另一种实现方法是使用linq语句。推荐的答案在。net core2.0中对我不起作用。然而,我能够弄清楚,如果你使用更新的技术,下面也可以工作。

[HttpPost]
public ActionResult Report(FormCollection collection)
{
    var listofIDs = collection.ToList().Select(x => x.ToString());
    List<Dinner> dinners = new List<Dinner>();
    dinners = repository.GetDinners(listofIDs);
    return View(dinners);
}
  public List<int> ConvertStringListToIntList(List<string> list) 
  {
     List<int> resultList = new List<int>();
     for (int i = 0; i < list.Count; i++)
        resultList.Add(Convert.ToInt32(list[i]));

     return resultList;
  }

使用Linq…

List<string> listofIDs = collection.AllKeys.ToList();  
List<int> myStringList = listofIDs.Select(s => int.Parse(s)).ToList();