更新记录:
MyEntityFramework ef = new MyEntityFramework();
User user = ef.User.FirstOrDefault(p => p.Oid == 20);
user.UserName = "叶兵";
ef.SaveChanges();
现在想实现的是user是动态的,p=>p.Oid == 20也是动态生成的,请问有没有高手能实现???
实体模型 反射
------解决方案--------------------------------------------------------
Func<User, bool> express = p => p.Oid == 20;
ef.User.FirstOrDefault(express);
------解决方案--------------------------------------------------------
关于动态查询 网上流传一段这样的代码。。但是我一直都没用过...
public static class DynamicLinqExpressions
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);