我有一个对象列表(RunSummary
),按UniqueId
列排序,然后是RunNo
列。但UniquId
个对象的类型为String
。我想按UniqueId
排序Integer
个对象(不是1,10,11,3而是1,3,10,11)。这是我的代码:
public List<RunSummary> GetRunSummariesForUniqueIds(List<String> uniqueIds)
{
using (var context = new ELSORegistryEntities())
{
context.Configuration.ProxyCreationEnabled = false;
List<RunSummary> runSummaries = context.RunSummaries
.Where(param => uniqueIds.Contains(param.UniqueId))
.OrderBy(param => param.UniqueId)
.ThenBy(param => param.RunNo)
.ToList<RunSummary>);
return runSummaries;
}
}
我试过
List<RunSummary> runSummaries = context.RunSummaries
.Where(param => uniqueIds.Contains(param.UniqueId))
.OrderBy(param => Convert.ToInt32(param.UniqueId))
.ThenBy(param => param.RunNo)
.ToList<RunSummary>();
但它不起作用。如何在LINQ中排序?
答案 0 :(得分:2)
你不能在查询中使用Convert.ToInt32,你应该在查询后订购
List<RunSummary> runSummaries = context.RunSummaries.Where(param => uniqueIds.
Contains(param.UniqueId)).ToList<RunSummary>()
.OrderBy(param => Convert.ToInt32(param.UniqueId))
.ThenBy(param => param.RunNo).ToList();