2011-06-01 174 views
114

我的問題是這個問題的一部分:如何把List <string>轉換成列表<int>?

我從表單中收到一個id的集合。我需要獲取鍵,將它們轉換爲整數並從數據庫中選擇匹配的記錄。

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

回答

270
listofIDs.Select(int.Parse).ToList() 
+0

異常引發 - LINQ to Entities無法識別方法'Int32 IndexOf(Char)'方法,並且此方法無法轉換爲商店表達式。 .net 4.0 – markthewizard1234 2016-05-31 08:22:28

+1

@ markthewizard1234如果你的'listofIDs'是一個'IQueryable ',那麼會發生這種情況,它還沒有被執行。在你進行轉換之前先用'ToList()'執行它:'listofIDs.ToList()。Select(int.Parse).ToList()' – 2016-06-13 19:59:41

+1

didn'it for me – fnc12 2016-10-15 08:44:47

31

使用LINQ ...

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

使用LINQ:

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

這是最簡單的方法,我想:

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

我經常在lambda表達式中看到這種模式:_x => someMethod(x)_。你確實知道你應該用_someMethod_替換它。由於你的例子變成_listOfStrings.Select (Convert.ToInt32); _。這可能更可讀,更快,更短,我已經提到更優雅?所有其他人回答這個問題也是如此。 – JBSnorro 2011-06-01 13:05:22

+0

省略箭頭語法時出現錯誤。這個工作:'List sss = new List ();''IEnumerable test1 = sss.Select (x => Convert.ToInt32(x));'但是這不是:'IEnumerable test2 = sss.Select (Convert.ToInt32);'錯誤是在'字符串,int,int'和'string,int'版本的Select之間的模糊調用。 – goodeye 2012-07-11 23:45:09

-1
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; 
    } 
+3

考慮添加一些關於您的答案的文字說明,而不是僅提供代碼。 – carlosfigueira 2014-07-17 18:55:14

-2
list<int> integersList = decimalList.cast<int>().ToList() 

if nullable type then then place'''未來它的類型,像

list<int?> integersList = decimalList.cast<int?>().ToList() 
+2

他有一個字符串列表,它們不能被轉換爲int。 – Vache 2015-02-18 22:07:55

3

的我知道這是舊的文章,但我認爲這是一個很好的補充: 您可以使用List<T>.ConvertAll<TOutput>

List<int> integers = strings.ConvertAll(s => Int32.Parse(s)); 
4

這裏是一個安全變體,可以過濾掉無效整數:

List<int> ints = strings 
    .Select(s => Int32.TryParse(s, out int n) ? n : (int?)null) 
    .Where(n => n.HasValue) 
    .Select(n => n.Value) 
    .ToList(); 

它採用C#7.0中引入的新out變量。

這其他變種返回那裏null條目插入無效整數可空整數的列表(即它保留了原始列表計數):

List<int?> nullableInts = strings 
    .Select(s => Int32.TryParse(s, out int n) ? n : (int?)null) 
    .ToList(); 
+0

好的答案!我不知道是誰低估了你的(爲什麼?),但是拿我的贊成票。 – 2018-01-09 12:41:24

+0

非常感謝! C#6.0的版本低於https://stackoverflow.com/a/48712010/2340825 – 2018-02-09 18:30:30

0

另一種方式來實現這一目標將使用LINQ語句。推薦的答案在我的.NetCore2.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); 
} 
2

What tryParse? 安全LINQ版本過濾掉無效的整數(對於C#6.0及以下):

List<int> ints = strings 
    .Select(s => { int i; return int.TryParse(s, out i) ? i : (int?)null; }) 
    .Where(i => i.HasValue) 
    .Select(i => i.Value) 
    .ToList(); 

信貸奧利維爾Jacot-Descombes爲理念和C#7.0的版本。

相關問題