我的EF模型如下:
public class MetaData
{
public ICollection<MetaDataDictionary> MetaDataDictionary { get; set; }
}
public partial class MetaDataDictionary
{
public string Key { get; set; }
public string Value { get; set; }
}
public List<MetaData> MetaDataList { get; set; }
...所以我可以有一个像这样的实例:
MetaDataList
MetaData1
MetaDataDictionary
{ key = "aaa", value = "4" }
{ key = "bbb", value = "5" }
{ key = "ccc", value = "6" }
MetaData2
MetaDataDictionary
{ key = "aaa", value = "1" }
{ key = "bbb", value = "2" }
{ key = "ccc", value = "3" }
目标是根据MetaDataDictionary的特定键值(例如键“aaa”)对MetaDataList进行排序,输出应为:
MetaDataList
MetaData1
MetaDataDictionary
{ key = "aaa", value = "1" }
{ key = "bbb", value = "2" }
{ key = "ccc", value = "3" }
MetaData2
MetaDataDictionary
{ key = "aaa", value = "4" }
{ key = "bbb", value = "5" }
{ key = "ccc", value = "6" }
如您所见,它现在按键“bbb”的值排序。
我试过了:
var sorted = metaDataList.OrderBy(md => md.MetaDataDictionary.Select(mdd => mdd.Key == "aaa").Single()).ToList();
...但它抱怨'DbSortClause表达式必须具有可比较的类型'。
我是如何做到这一点的?
谢谢!
编辑:试过这个:
var sorted = metaDataList.OrderBy(md => md.MetaDataDictionary.Where(mdd => mdd.Key == "aaa").Select(mdd => mdd.Value).FirstOrDefault()).ToList();
似乎有效,但如果有人有更好的解决方案,请随时分享:)
答案 0 :(得分:0)
也许这个?
metaDataList.OrderBy(md => md.MetaDataDictionary.First(mdd => mdd.Key == "aaa").Value)
.ToList();
我还没有测试过它,但相信它应该可行。