如何从匿名Linq.Expressions.Expression返回单个值

时间:2016-09-27 20:36:28

标签: c# linq

当使用LINQ的Entity Framework时,我可以编写如下代码:
var a = context.Employee.Where(x => x.Name.Equals("Michael")).FirstOrDefault();

我很好奇如何使用匿名linq表达式创建自定义方法。 我有一个模型类和方法如下

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

static T Single<T>(Expression<Func<T, bool>> predicate) where T : class
{
    //This is from the database
    List<Employee> employees = new List<Employee>
    {
        new Employee { Id = 1, Name = "Michael" },
        new Employee { Id = 2, Name = "Derek" }
    };

    //I have no idea how to return single value of employees

    //var expression = (BinaryExpression)predicate.Body;
    //var left = expression.Left.Type.Name;
    //Func<T, bool> func = predicate.Compile();
    //T value = method(html.ViewData.Model);
    //T t = (T)Convert.ChangeType(a, typeof(T));
    //Employee emp = (Employee)Convert.ChangeType(t, typeof(Employee));

    //var body = predicate.Body;

    //var prop = (PropertyInfo)((MemberExpression)predicate.Body).Member;
    //var propValue = prop.GetValue(func, null);

    //var parameter = Expression.Parameter(typeof(T));
    //var property = Expression.Property(parameter, expression.Member.Name);
    //var equal = Expression.Equal(property, Expression.Constant(propValue));
    //var lambda = Expression.Lambda<Func<T, bool>>(equal, parameter);


    return null;
}

调用上述方法应该像是

Employee emp = Single<Employee>(d => d.Name == "Michael");

2 个答案:

答案 0 :(得分:0)

您可以直接致电FirstOrDefault

var a = context.Employee.FirstOrDefault(x => x.Name.Equals("Michael")); 

或者在你的功能中你可以使用

return employees.FirstOrDefault(predicate);

答案 1 :(得分:0)

尝试使用IQuerable.Where代替IEnumerable.Where 第一个使用Expression<Func<TSource, Boolean>>作为参数。第二次使用Func<TSource, Boolean>

所以你需要使用AsQuerable()然后Where() 所以在你的情况下它应该看起来像

return employees.AsQuerable().Where(predicate).FirstOrDefault()

如果您要传递delegate而不是Expression,则需要将方法更改为static T Single<T>(Func<T, bool> predicate) where T : class,然后 return employees.FirstOrDefault(predicate)

所以你可以这样使用它

Employee emp = Single<Employee>(d => d.Name == "Michael");