在实体框架使用动态where子句 [英] Using dynamic where clauses in Entity Framework

查看:162
本文介绍了在实体框架使用动态where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重新设计使用Raptier,最初建立了一个数据访问层。接受where子句作为参数Raptier generats方法,在一个存储过程进行传递。我真的需要保留现有mesthos签名,所以我的新DAL需要接受where子句为好。我想用越涨最新数据访问技术和工艺,因此想使用实体框架的.NET 4.0。

I am trying to re-design a data access layer that was originally built using Raptier. Raptier generats methods that accept a where clause as a parameter to be passed in to a stored proc. I really need to retain the existing mesthos signatures, so my new DAL needs to accept where clauses as well. I want to use the more up-to-date data access technologies and techniques, so was thinking about using Entity Framework from .Net 4.0.

然而,它不看起来像我可以接受动态where子句没有实施一些激烈的reoutines他们解析为兰巴表达式。有什么我已经错过了?难道我的运气与实体框架?

However, it doesn't look like I can accept dynamic where clauses without implementing some intense reoutines to parse them into lamba expressions. Is there something I've missed? Am I out of luck with Entity Framework?

谢谢,
马克

推荐答案

进来两种念头。

1)。 动态LINQ ,它允许您定义,操作员为字符串。

1). Dynamic LINQ, which allows you to define where operators as strings.

var result = Northwind.Products
    .Where("CategoryId=2 And UnitPrice>3")
    .OrderBy("SupplierId");



2)。使用的东西作为 EntityFilter< T> (参见代码这里),它允许你定义这样的过滤器:

2). Use something as the EntityFilter<T> (see code here), which allows you to define a filter like this:

IEntityFilter<Person> entityFilter =
    from person in EntityFilter<Person>.AsQueryable()
    where person.Name.StartsWith("a")
    where person.Id < 100
    select person;

您可以提供 IEntityFilter<人> 一个企业的方法,将能够过滤查询:

You can supply the IEntityFilter<Person> to a business method that will be able to filter that query:

public static Person[] GetAllPersons(
    IEntityFilter<Person> filter)
{
    using (var db = ContextFactory.CreateContext())
    {
        IQueryable<Person> filteredList =
            filter.Filter(db.Persons);

        return filteredList.ToArray();
    }
}

这篇关于在实体框架使用动态where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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