如何将枚举与 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
}
}
I would like to use enumerations in my dynamic LINQ queries.
Is it possible, and if, how?
Consider the code bellow:
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了同样的问题,并尝试了@Steve Wilkes 指定的标记答案,但它对我不起作用!
然后我发现动态 LINQ 在同一个包中有一个 HTML 文档,其中提到可以将枚举指定为字符串文字。
这对我有用。
I encountered this same issue and tried the marked answer specified by @Steve Wilkes but it didn't work for me !!
Then I discovered that dynamic LINQ has an HTML documentation in the same package which mentioned that Enums can be specified as String Literals.
Which worked for me.
这有效:
This works:
另外还有另一个变体使用参数
in addition yet another variant use parameter
这应该可行
为什么在这种情况下需要动态 linq?您期望的输出
根据我的偏好,应避免使用容易出错的字符串。如果您的类或属性名称发生更改,您将无法找到错误,直到遇到它。
而是使用表达式
您可以创建表达式来决定要使用哪个字符串过滤器。最好创建一个静态类或 switch 语句,它为您提供不同类型的表达式,您可以将其用作 where 参数。
This should work
Why do you need dynamic linq in this case? What output you expect
To my preference, use of error prone string should be avoided. If your class or property name changed, you won't be able to find the error until you encounter it.
Rather use expression
You can create the expression where you decide which string filter to be used. Better create a static class or may be switch statement which gives you different type of expression which you can use as where argument.
要将新的 Enum 类型添加到动态 linq,您必须添加以下代码:
在动态的预定义类型中
这对我来说就是工作;
To add a new Enum type to dynamic linq, you must add the following code :
in predefinedTypes of dynamic
That's work for me;