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