在LINQ C#中将字符串列表排序为整数#

时间:2016-06-20 16:40:04

标签: c# linq sorting entity-framework-6

我有一个对象列表(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中排序?

1 个答案:

答案 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();