我在C#中關於System.Random命令的問題。將一串字符串加入到單個字符串中
我有這樣的MVC 4項目的查詢:
public JsonResult GetQuestions()
{
...
var rnd = new Random();
var selectedData = data.Select(y => new
{
...,
qAnswers = ((y.qA1 != null ? "ab" : "") +
(y.qA2 != null ? "cd" : "") +
(y.qA3 != null ? "ef" : "") +
(y.qA4 != null ? "gh" : "") +
(y.qA5 != null ? "ij" : "")).OrderBy(item => rnd.Next())
});
return Json(selectedData, JsonRequestBehavior.AllowGet);
}
由於查詢的結果,我想看到的東西,如:
ijcdefabgh
但結果是:
["i","a","c","d","g","h","e","f","b","j"]
你知道我的錯誤在哪裏嗎?或者我如何解決它?
你的意思是你想要的是一個字符串,但你有一個字符串數組? –
這個問題有兩個部分。首先,所需輸出不同於當前輸出,這是因爲隨機排列你的列。你需要刪除它。其次你得到的是需要串聯的字符串數組,以獲得所需的輸出使用'string.Join' –
這將有助於如果這是一個https://stackoverflow.com/help/mcve – mjwills