Linq表达式,用于基于静态值对实体进行排序 [英] Linq expression for ordering entities based on static values

查看:81
本文介绍了Linq表达式,用于基于静态值对实体进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在首先使用EF代码进行ASP.NET MVC项目,我需要构建一个Linq expression以便基于静态字典值对Item实体进行排序.

I'm working on an ASP.NET MVC project using EF code first and I would need to build a Linq expression in order to order Item entities based on a static dictionary values.

public partial class Item
{
    public enum TypeE
    {
        Type1,
        Type2,
        Type3,
        Type4,
    }

    public TypeE Type { get; set; } // Mapped database column

    public static Dictionary<TypeE, int> MyDic = new Dictionary<TypeE, int>()
    {
        { TypeE.Type1, 42 },
        { TypeE.Type2, 16 },
        { TypeE.Type3, 0 },
        { TypeE.Type4, 34 },
    };
}

我的最终目标是在Linq to entities中工作的某种方法,这将使我能够实现myEntities.OrderBy(i => Item.MyDic[i.Type])之类的东西.

My final aim would be some method working in Linq to entities and that would allow me to achieve something like myEntities.OrderBy(i => Item.MyDic[i.Type]).

我要精确地说,我不能使用AsEnumerable()或枚举实体集合的其他任何东西,我真的需要直接在Linq to entities中工作的东西.
我也想避免在数据库中创建引用表,我真的在寻找Linq expression.

I've to precise that I can not use AsEnumerable() or anything else enumerating the entities collection, I really need something working directly in Linq to entities.
I also would like to avoid creating reference tables in the database, I'm really looking for a Linq expression.

几天前,我问了一个非常类似的问题,即如何通过枚举描述对实体进行排序以及Ivan Stoev( https://stackoverflow.com/a/40203664/2828106 )完美实现了我想要的功能.
如果有一种方法可以为这种新目的重用这种逻辑,那就太好了,但是我还没有做足够的实验,结果在尝试时遇到了无限循环.

A few days ago, I asked a quite similar question about how to sort entities by an enum description and the answer given by Ivan Stoev (https://stackoverflow.com/a/40203664/2828106) perfectly achieved what I wanted.
If there was a way to reuse this kind of logic for this new purpose this would be great but I was not experimented enough, I ended up with an infinite loop while trying.

非常感谢.

推荐答案

以下是用于字典的相同方法:

Here is the same approach utilized for dictionary:

public static class Expressions
{
    public static Expression<Func<TSource, int>> DictionaryOrder<TSource, TKey, TOrder>(Expression<Func<TSource, TKey>> source, IReadOnlyDictionary<TKey, TOrder> by)
    {
        var body = by
            .OrderBy(entry => entry.Value)
            .Select((entry, ordinal) => new { entry.Key, ordinal })
            .Reverse()
            .Aggregate((Expression)null, (next, item) => next == null ? (Expression)
                Expression.Constant(item.ordinal) :
                Expression.Condition(
                    Expression.Equal(source.Body, Expression.Constant(item.Key)),
                    Expression.Constant(item.ordinal),
                    next));

        return Expression.Lambda<Func<TSource, int>>(body, source.Parameters[0]);
    }
}

和示例用法:

var order = Expressions.DictionaryOrder((Item x) => x.Type, Item.MyDic);

这篇关于Linq表达式,用于基于静态值对实体进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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