9

我想在我的动态 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
    }
}
4

5 回答 5

21

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

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

这对我有用。

于 2012-06-21T08:57:35.703 回答
4

这有效:

houses.AsQueryable<House>()
    .Where("MainRoom.Type = ConsoleApplication2.RoomType.Kitchen")
于 2011-08-16T10:29:54.083 回答
1

这应该工作

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 参数。

于 2011-08-16T10:23:37.857 回答
1

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

var aRoomsHouses = houses.AsQueryable<House>().Where("MainRoom.Type = @0",RoomType.Kitchen);
于 2013-11-07T13:59:21.563 回答
-1

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

typeof(Enum),
typeof(T)

T : Enum type

在预定义的动态类型中这对我有用;

于 2013-03-22T13:06:10.463 回答