使用 LINQ 的动态表达式。如何找到厨房?
我尝试实现一个用户动态过滤器,其中使用选择一些属性,选择一些运算符并选择值。
由于我还没有找到这个问题的答案,因此我尝试使用 LINQ 表达式。
主要是我需要识别所有主要房间是厨房的房屋(任何感觉,我知道)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
//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 });
//var kitchens = houses.AsQueryable<House>().Where("MainRoom.Type = RoomType.Kitchen");
//Console.WriteLine("kitchens count = {0}", kitchens.Count());
var houseParam = Expression.Parameter(typeof(House), "house");
var houseMainRoomParam = Expression.Property(houseParam, "MainRoom");
var houseMainRoomTypeParam = Expression.Property(houseMainRoomParam, "Type");
var roomTypeParam = Expression.Parameter(typeof(RoomType), "roomType");
var comparison = Expression.Lambda(
Expression.Equal(houseMainRoomTypeParam,
Expression.Constant("Kitchen", typeof(RoomType)))
);
// ???????????????????????? DOES NOT WORK
var kitchens = houses.AsQueryable().Where(comparison);
Console.WriteLine("kitchens count = {0}", kitchens.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)
但您必须先设置房间的
RoomType
属性。好的,编辑:
所以你必须重新定义:
然后,当你使用它时:
编辑#2:
好的,你开始:
编辑#3:当然,为了你的需要,我现在没有想法。我给你最后一个:
在 String 类型上声明一个扩展方法:
然后在该表达式中使用它,例如:
那是因为显然枚举的处理方式不同。该扩展将使其他类型的字符串保持不变。缺点:您必须在那里添加另一个
typeof()
。But you must set the
RoomType
property on the rooms before.Ok, edit:
so you must redefine:
Then, when you use it:
Edit #2:
Ok, here you go:
Edit #3: Of, for your needs, I am out of ideas for now. I give you one last one:
Declare an extension method on the String type:
Then use it in that expression like:
That's because apparently enums are treated differently. That extension will leave the string unaltered for other types. Drawback: you have to add another
typeof()
there.Where
方法采用Func
或Expression>
作为参数,但变量comparison
的类型为LambdaExpression
,不匹配。您需要使用该方法的另一个重载:The
Where
method takes aFunc<House, bool>
or aExpression<Func<House, bool>>
as the parameter, but the variablecomparison
is of typeLambdaExpression
, which doesn't match. You need to use another overload of the method:我不会以这种方式构建 where 子句 - 我认为它比您的需求所需的更复杂。相反,您可以组合这样的 where 子句:
我测试了这个,诚实:)
I wouldn't build the where clause in that way - I think it's more complex than it needs to be for your needs. Instead, you can combine where clauses like this:
I tested this one, honest :)
这个怎么样
what about this
添加以下代码:
要向动态 Linq 添加新的
Enum
类型,您必须在预定义的动态类型中 这对我有用。To add a new
Enum
type to dynamic Linq, you must add the following code :in predefined types of dynamic. That works for me.