如何将枚举与 Dynamic Linq 结合使用?

发布于 2024-11-29 15:47:17 字数 1928 浏览 1 评论 0原文

我想在动态 LINQ 查询中使用枚举。

是否可能,如果,如何?

考虑下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Room aRoom = new Room() { Name = "a Room" };
            Room bRoom = new Room() { Name = "b Room" };
            Room cRoom = new Room() { Name = "c Room" };

            House myHouse = new House
            {
                Rooms = new List<Room>(new Room[] { aRoom }),
                MainRoom = aRoom
            };
            House yourHouse = new House()
            {
                Rooms = new List<Room>(new Room[] { bRoom, cRoom }),
                MainRoom = bRoom
            };
            House donaldsHouse = new House()
            {
                Rooms = new List<Room>(new Room[] { aRoom, bRoom, cRoom }),
                MainRoom = aRoom
            };

            var houses = new List<House>(new House[] { myHouse, yourHouse, donaldsHouse });

            // MainRoom.Name = \"a Room\" and Rooms.Count = 3 or 
            // ?????????????????????????
            var aRoomsHouses = houses.AsQueryable<House>().Where("MainRoom.Type = \"RoomType.Kitchen\"");

            Console.WriteLine("aRoomsHouses count = {0}", aRoomsHouses.Count());
            Console.ReadKey();
        }
    }

    public class House
    {
        public string Address { get; set; }
        public double Area { get; set; }
        public Room MainRoom { get; set; }
        public List<Room> Rooms { get; set; }
    }

    public class Room
    {
        public double Area { get; set; }
        public string Name { get; set; }
        public RoomType Type { get; set; }
    }

    public enum RoomType
    {
        Kitchen,
        Bedroom,
        Library,
        Office
    }
}

I would like to use enumerations in my dynamic LINQ queries.

Is it possible, and if, how?

Consider the code bellow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Room aRoom = new Room() { Name = "a Room" };
            Room bRoom = new Room() { Name = "b Room" };
            Room cRoom = new Room() { Name = "c Room" };

            House myHouse = new House
            {
                Rooms = new List<Room>(new Room[] { aRoom }),
                MainRoom = aRoom
            };
            House yourHouse = new House()
            {
                Rooms = new List<Room>(new Room[] { bRoom, cRoom }),
                MainRoom = bRoom
            };
            House donaldsHouse = new House()
            {
                Rooms = new List<Room>(new Room[] { aRoom, bRoom, cRoom }),
                MainRoom = aRoom
            };

            var houses = new List<House>(new House[] { myHouse, yourHouse, donaldsHouse });

            // MainRoom.Name = \"a Room\" and Rooms.Count = 3 or 
            // ?????????????????????????
            var aRoomsHouses = houses.AsQueryable<House>().Where("MainRoom.Type = \"RoomType.Kitchen\"");

            Console.WriteLine("aRoomsHouses count = {0}", aRoomsHouses.Count());
            Console.ReadKey();
        }
    }

    public class House
    {
        public string Address { get; set; }
        public double Area { get; set; }
        public Room MainRoom { get; set; }
        public List<Room> Rooms { get; set; }
    }

    public class Room
    {
        public double Area { get; set; }
        public string Name { get; set; }
        public RoomType Type { get; set; }
    }

    public enum RoomType
    {
        Kitchen,
        Bedroom,
        Library,
        Office
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里泪两行 2024-12-06 15:47:17

我遇到了同样的问题,并尝试了@Steve Wilkes 指定的标记答案,但它对我不起作用!
然后我发现动态 LINQ 在同一个包中有一个 HTML 文档,其中提到可以将枚举指定为字符串文字。

houses.AsQueryable<House>().Where("MainRoom.Type = \"Kitchen\"")

这对我有用。

I encountered this same issue and tried the marked answer specified by @Steve Wilkes but it didn't work for me !!
Then I discovered that dynamic LINQ has an HTML documentation in the same package which mentioned that Enums can be specified as String Literals.

houses.AsQueryable<House>().Where("MainRoom.Type = \"Kitchen\"")

Which worked for me.

醉生梦死 2024-12-06 15:47:17

这有效:

houses.AsQueryable<House>()
    .Where("MainRoom.Type = ConsoleApplication2.RoomType.Kitchen")

This works:

houses.AsQueryable<House>()
    .Where("MainRoom.Type = ConsoleApplication2.RoomType.Kitchen")
汹涌人海 2024-12-06 15:47:17

另外还有另一个变体使用参数

var aRoomsHouses = houses.AsQueryable<House>().Where("MainRoom.Type = @0",RoomType.Kitchen);

in addition yet another variant use parameter

var aRoomsHouses = houses.AsQueryable<House>().Where("MainRoom.Type = @0",RoomType.Kitchen);
不羁少年 2024-12-06 15:47:17

这应该可行

houses.AsQueryable<House>().Where(rs=>rs.MainRoom.Type == RoomType.Kitchen);

为什么在这种情况下需要动态 linq?您期望的输出

根据我的偏好,应避免使用容易出错的字符串。如果您的类或属性名称发生更改,您将无法找到错误,直到遇到它。

而是使用表达式

    Expression<Func<House, bool>> 
        filter = (p) => p.MainRoom.Type == RoomType.Kitchen; 
        filter = (p) => p.MainRoom.Area > 200;
        filter = (p) => p.Rooms.Sum(rs => rs.Area) > 500;
        filter = (p) => p.Address.Contains("abc");
        filter = (p) => p.Area > 200;
        ...
    var aRoomsHouses = houses.AsQueryable<House>().Where(filter);

您可以创建表达式来决定要使用哪个字符串过滤器。最好创建一个静态类或 switch 语句,它为您提供不同类型的表达式,您可以将其用作 where 参数。

This should work

houses.AsQueryable<House>().Where(rs=>rs.MainRoom.Type == RoomType.Kitchen);

Why do you need dynamic linq in this case? What output you expect

To my preference, use of error prone string should be avoided. If your class or property name changed, you won't be able to find the error until you encounter it.

Rather use expression

    Expression<Func<House, bool>> 
        filter = (p) => p.MainRoom.Type == RoomType.Kitchen; 
        filter = (p) => p.MainRoom.Area > 200;
        filter = (p) => p.Rooms.Sum(rs => rs.Area) > 500;
        filter = (p) => p.Address.Contains("abc");
        filter = (p) => p.Area > 200;
        ...
    var aRoomsHouses = houses.AsQueryable<House>().Where(filter);

You can create the expression where you decide which string filter to be used. Better create a static class or may be switch statement which gives you different type of expression which you can use as where argument.

谈情不如逗狗 2024-12-06 15:47:17

要将新的 Enum 类型添加到动态 linq,您必须添加以下代码:

typeof(Enum),
typeof(T)

T : Enum type

在动态的预定义类型中
这对我来说就是工作;

To add a new Enum type to dynamic linq, you must add the following code :

typeof(Enum),
typeof(T)

T : Enum type

in predefinedTypes of dynamic
That's work for me;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文