gpt4 book ai didi

c# - 使用 LINQ 的动态表达式。如何找到厨房?

转载 作者:太空狗 更新时间:2023-10-29 17:56:36 26 4
gpt4 key购买 nike

我尝试实现一个用户动态过滤器,其中使用选择一些属性,选择一些运算符并选择值。

因为我还没有找到 this question 的答案,我尝试使用 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
}
}

最佳答案

var kitchens = from h in houses
where h.MainRoom.Type == RoomType.Kitchen
select h;

但是您必须先在房间上设置RoomType 属性。

好的,编辑:

所以你必须重新定义:

var comparison = Expression.Lambda<Func<House, bool>>(...

然后,当你使用它时:

var kitchens = houses.AsQueryable().Where(comparison.Compile());

编辑#2:

好的,给你:

var roomTypeParam = Expression.Parameter(typeof(RoomType), "roomType");



// ???????????????????????? DOES NOT WORK
var comparison = Expression.Lambda<Func<House, bool>>(
Expression.Equal(houseMainRoomTypeParam,
Expression.Constant(Enum.Parse(typeof(RoomType), "Kitchen"), typeof(RoomType))), houseParam);



// ???????????????????????? DOES NOT WORK
var kitchens = houses.AsQueryable().Where(comparison);

编辑#3:当然,为了您的需要,我暂时没有想法。我给你最后一个:

在 String 类型上声明一个扩展方法:

internal static object Prepare(this string value, Type type)
{
if (type.IsEnum)
return Enum.Parse(type, value);

return value;
}

然后在该表达式中使用它,例如:

Expression.Constant("Kitchen".Prepare(typeof(RoomType)), typeof(RoomType))

那是因为显然枚举的处理方式不同。该扩展将使其他类型的字符串保持不变。缺点:你必须在那里添加另一个 typeof()

关于c# - 使用 LINQ 的动态表达式。如何找到厨房?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7077056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com