我想讓SetCloud()根據不同的屬性查詢List,然後將它存儲在Dictionary中。將屬性傳遞給在LINQ查詢中使用的方法
OpenPosition對象有3個屬性:Entry_Price,Stop_Loss,Take_Profit。這是Stop_Loss的硬編碼版本。
private Dictionary<double, PriceLevel> SetCloud(List<OpenPosition> positions, string currency, int trade_Type)
{
Dictionary<double, PriceLevel> levels = new Dictionary<double, PriceLevel>();
var priceLevels = from position in positions // query OpenPosition objects from the List<>
group position by position.Stop_Loss into groups
select groups;
//add stuff to the Dicionary
return levels;
}
我想在簽名中傳遞所需的OpenPosition屬性,以便我可以在LINQ查詢中使用它。錯誤的僞代碼版本是
SetCloud(....,int trade_Type, object propertyName)
{
var priceLevels = from position in positions // query OpenPosition objects from the List<>
group position by position.propertyName into groups
select groups;
}
我希望傳達爲什麼卡住了。我不知道使用哪些工具來完成此操作。其他帖子介紹瞭如何查詢屬性名稱的對象,但字符串值在LINQ查詢中對我沒有任何好處。
此線程應該回答你的問題:HTTP:// stackoverflow.com/questions/17678197/linq-grouping-dynamically – 2014-11-21 17:11:12