2016-03-11 58 views
1

我無法找出一個辦法讓這個正常工作秩序。我有一個整數列表..和那些整數是在我的字符串列表中的字符串的計數。排序列表<int>基於列表<string>

我正在使用MVC圖表助手,我的問題是我的yValues沒有與我的xValues正確對齊。

例如,我的List<string>保存值:"Beginner""MOTR","Advanced"。我使用這個列表來訂購下面的列表。

而我的數據庫包含一堆記錄,每個記錄是"Beginner""MOTR""Advanced"。所以我有一個foreach循環收集爲每個樓層計數,因此它可以是動態的。但是,當執行foreach循環時,它會按照字母順序放置關卡。

我也有一個叫做Rank是有水平的屬性(局部視圖目的)

這裏類是我:

List<string>lstLevelsInOrder = new List<string> {"Beginner", "MOTR", "Advanced"}; 

List<int> emptyListIntegers = new List<int>(); 
List<string> lstLevels = database.testdb.Where(x => x.deleted == false).Select(x => x.Level).Distinct().ToList(); 

foreach(var item in lstLevels) 
{ 
    Rank rank = new Rank(); 
    rank.level = item; 
    emptyListIntegers.Add(database.testdb.Where(x => x.Level == rank.level && x.deleted == false).Count()); 
} 

var key = new Chart(width: 800, height: 800, theme: ChartTheme.Blue) 
    .SetXAxis() 
    .SetYAxis() 
    .AddTitle() 
    .AddSeries(
    xValue: lstLevelsInOrder, 
    yValues: emptyListIntegers 
    ); 

return File(key.ToWebImage().GetBytes(), "image/jpeg"); 

所以剛纔爭論的緣故,讓我們說的算對於初學者是10,MOTR是20,高級是5.

當那個foreach循環執行emptyListIntegers將填充爲5,10,20(由於字母順序)

所以當我看到我的圖表.. x軸是「初級」爲5的y值,「MOTR」爲10的y值,「高級」與20

一個y值

emptyListIntegers基礎上,lstLevelsInOrder如何排序?

回答

5

您必須由string在其他列表中的索引適當命令:

List<string> lstLevels = database.testdb 
    .Where(x => ! x.deleted) 
    .AsEnumerable() // force Linq-To-Objects 
    .OrderBy(x => lstLevelsInOrder.IndexOf(x.Level)) 
    .Select(x => x.Level) 
    .Distinct() 
    .ToList(); 
+0

當我嘗試,我得到的.OrderBy和.IndexOf,當我試圖把x.Level我我無法。我看不到我的屬性名稱。我得到的所有的方法..但沒有屬性名 –

+0

@BviLLe_Kid:當然,你在選擇之前,不同的琴絃,我已經固定我的答案現在 –

+0

得到它現在哈哈你搖滾的人!非常感謝! –