实体框架4.1 object.property = value的简单动态表达式 [英] Entity Framework 4.1 simple dynamic expression for object.property = value

查看:86
本文介绍了实体框架4.1 object.property = value的简单动态表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一种方法可以使用表达式和Lambdas来实现这一点,但是我很难拼凑在一起。所有我需要的是一种方法,将动态查询Entity Framework DBSet对象,找到具有给定名称的属性与该值匹配的行。



我的上下文: p>

  public class MyContext:DbContext 
{
public IDbSet< Account> Accoounts {get {return Set< Account>(); }}
}

我想要写的方法:

  public T Get< T>(string property,object value):其中T是Account 
{...}

我宁可不必使用动态SQL来完成这一切,所以不需要建议,因为我已经知道这是可能的。我真正寻找的是使用表达式和Lambdas来实现这一点的一些帮助



提前感谢,我知道它很简短,但它应该是非常不言自明的。评论如果需要更多信息

解决方案

我试图尽可能避免动态linq,因为linq的要点是强类型访问。使用动态linq是一个解决方案,但它正是linq目的的反对,它非常接近使用ESQL并构建来自sting连接的查询。无论如何,动态linq有时是实时保护程序(特别是涉及到复杂的动态排序),我成功地使用它在一个大型项目中与Linq-to-Sql。



什么我通常做的是定义一些 SearchCriteria 类,如:

  public class SearchCriteria 
{
public string Property1 {get;组; }
public int? Property2 {get;组; }
}

和辅助查询扩展方法如:

  public static IQueryable< SomeClass>过滤器(这个IQueryable< SomeClass>查询,SearchCriteria过滤器)
{
if(filter.Property1!= null)query = query.Where(s => s.Property1 == filter.Property1);
if(filter.Property2!= null)query = query.Where(s => s.Property2 == filter.Property2);
返回查询;
}

这不是通用的解决方案。再次,通用解决方案是对类共享某些行为的一些强类型处理。



更复杂的解决方案将是使用谓词构建器,并自己构建表达式树,而是再次构建表达式树仅通过连接字符串来构建ESQL查询的更复杂的方法。


I know there is a way to use Expressions and Lambdas to accomplish this but I having a hard time piecing it all together. All I need is a method that will dynamically query an Entity Framework DBSet object to find the row where the propery with the given name matches the value.

My context:

public class MyContext : DbContext
{
    public IDbSet<Account> Accoounts{ get { return Set<Account>(); } } 
}

The method that I'm looking to write:

public T Get<T>(string property, object value) : where T is Account
{...}

I would rather not have to use Dynamic SQL to accomplish this so no need to suggest it because I already know it's possible. What I'm really looking for is some help to accomplish this using Expressions and Lambdas

Thanks in advance, I know it's brief but it should be pretty self-explanatory. Comment if more info is needed

解决方案

I'm trying to avoid dynamic linq as much as possible because the main point of linq is strongly typed access. Using dynamic linq is a solution but it is exactly the oppose of the linq purpose and it is quite close to using ESQL and building the query from sting concatenation. Anyway dynamic linq is sometimes real time saver (especially when it comes to complex dynamic ordering) and I successfully use it in a large project with Linq-to-Sql.

What I usually do is defining some SearchCriteria class like:

public class SearchCriteria
{
     public string Property1 { get; set; }
     public int? Property2 { get; set; }
}

And helper query extension method like:

public static IQueryable<SomeClass> Filter(this IQueryable<SomeClass> query, SearchCriteria filter)
{
     if (filter.Property1 != null) query = query.Where(s => s.Property1 == filter.Property1);
     if (filter.Property2 != null) query = query.Where(s => s.Property2 == filter.Property2);
     return query;
}

It is not generic solution. Again generic solution is for some strongly typed processing of classes sharing some behavior.

The more complex solution would be using predicate builder and build expression tree yourselves but again building expression tree is only more complex way to build ESQL query by concatenating strings.

这篇关于实体框架4.1 object.property = value的简单动态表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆