我想在动态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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现这个如何在Linq where子句中指定动态字段名称?并得到一个编译器错误,说:
无法解析方法'Where(System.Linq.Expressions.LambdaExpression
public class Employee
{
public string Name { get; private set; }
public int Salary { get; private set; }
public Employee(string name, int salary)
{
Name = name;
Salary = salary;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在控制台应用程序的主要方法
var employees = new List<Employee>
{
new Employee("Bob", 45000),
new Employee("Jane", 25000),
new Employee("Jim", 5)
};
var eParam = Expression.Parameter(typeof(Employee), "e");
var comparison = Expression.Lambda(
Expression.LessThan(
Expression.Property(eParam, "Salary"),
Expression.Constant(40000)),
eParam);
var c = from e in employees.Where(comparison) // COMPILER ERROR …Run Code Online (Sandbox Code Playgroud)