c# - 如何在 Dynamic Linq 中使用枚举?

标签 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
    }
}

最佳答案

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

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

这对我有用。

关于c# - 如何在 Dynamic Linq 中使用枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7075816/

相关文章:

c# - FastMember ObjectReader 不适用于继承的接口(interface)

c# - 在LINQ扩展方法中指定通用类型的原因

c# - WPF DynamicDataDisplay - 在 Y 轴上放置字符串而不是数字

.net - 将自定义 .NET 代码与 DOTNETNUKE 一起使用

c# - 如何部署我的 3 层 wcf 服务

c# - 如何在 C# 中将 xml 文件中的值赋给 datagridviewtextbox?

.net - 表单发布 mvc3 .net 后跳转到错误

c# - 在 LINQ 中,如何为每个 ID 选择一项?

C# 在使用 LINQ to XML 时检查元素是否存在

c# - 在 c# mvc 中发送多维对象,并渲染单个属性到 c# mvc View