如何在Dynamic Linq中使用Enums?

时间:2011-08-16 09:00:05

标签: c# .net linq dynamic-linq

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

5 个答案:

答案 0 :(得分:17)

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

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

这对我有用。

答案 1 :(得分:4)

这有效:

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

答案 2 :(得分: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参数。

答案 3 :(得分:1)

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

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

答案 4 :(得分:-1)

要向动态linq添加新的枚举类型,您必须添加以下代码:

typeof(Enum),
typeof(T)

T : Enum type

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