是linq中的新手..我堅持使用一種場景。即,如何在order by子句中使用動態變量
我必須根據用戶輸入對搜索結果進行排序。
用戶輸入是姓氏,名字和標題。對於輸入3下拉列表,我必須根據所選值對結果進行排序。
我試圖
order = Request["orders"].Split(',');
var param = order[0];
var p1 = typeof(Test).GetProperty(param);
param = order[1];
var p2 = typeof(Test).GetProperty(param);
param = order[2];
var p3 = typeof(Test).GetProperty(param);
model.Test = (from tests in model.Test
select tests).
OrderBy(x => p1.GetValue(x, null)).
ThenBy(x => p2.GetValue(x, null)).
ThenBy(x => p3.GetValue(x, null));
但它沒有工作。
我想QRY等預先這個
from tests in model.Test
select tests).OrderBy(x => x.lastname).
ThenBy(x => x.firstname).ThenBy(x => x.Title);
order[0]== lastname but how can i use it in the place of OrderBy(x => x.order[0])..?
感謝。
[Linq Order by when name name is dynamic and as a string as a string to a function]可能重複(http://stackoverflow.com/questions/8398453/linq-order-by-when-column-name-is-dynamic作爲一個字符串到函數) –