通过单独的`List< string>`变量对列进行排序 [英] Order column by a separate `List<string>` variable

查看:67
本文介绍了通过单独的`List< string>`变量对列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过另一个列表中元素的索引对实体框架查询的结果进行排序.

I need to sort the results of an entity framework query by the index of elements in another list.

我尝试了在其他地方找到的建议,例如

I tried the suggestion found elsewhere, like

.ThenBy(m=>list.IndexOf(m.Term))

但是我收到HTTP 500错误,所以我想知道是否丢失了某些东西.调试时,我会收到此内部异常.

but I get an HTTP 500 error, so I'm wondering if I'm missing something. When debugging, I get this inner exception.

LINQ to Entities无法识别方法'Int32 IndexOf(System.String)'方法,并且此方法无法转换 进入商店表达式.

LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String)' method, and this method cannot be translated into a store expression.

我特别在考虑这种情况.

In particular, I'm thinking of this scenario.

private IEnumerable<MiaLog1A> PopulateQuery(string selectedCampus)
{
    var list = new List<string> {"No Longer Alphabetical","Fall","Mid","Spring"};
    return _db.MiaLog1A.Where(m => m.Campus == selectedCampus)
            .OrderBy(m => m.StudentName)
            .ThenBy(m=>m.Term)  
                    /* I would like to sort "Term" by the 
                     * order of "list".
                     */
        .AsEnumerable();
}

我是否可以通过这种方式进行排序,还是仅限于通过字典排序或创建联接?

Is there a way I could sort in this manner, or am I restricted to ordering by a dictionary or creating a join?

推荐答案

这应该可以解决问题:

private IEnumerable<MiaLog1A> PopulateQuery(string selectedCampus)
{
    var list = new List<string> {"Fall","Mid","Spring"};
    return _db.MiaLog1A.Where(m => m.Campus == selectedCampus)
        .AsEnumerable()
        .OrderBy(m => m.StudentName)
        .ThenBy(m=> list.IndexOf(m.Term));
}

这篇关于通过单独的`List&lt; string&gt;`变量对列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆