如何在动态Linq中使用枚举

本文关键字:枚举 Linq 动态 | 更新日期: 2023-09-27 18:05:47

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

如何在动态Linq中使用枚举

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

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

这行得通:

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

另外还有一个变体use parameter

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

应该可以了

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

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

typeof(Enum),
typeof(T)
T : Enum type

中的predefinedTypes