在实体框架中使用动态 where 子句

发布于 2024-10-03 00:15:57 字数 258 浏览 0 评论 0原文

我正在尝试重新设计最初使用 Raptier 构建的数据访问层。 Raptier 生成的方法接受 where 子句作为要传递到存储过程的参数。我确实需要保留现有的 mesthos 签名,因此我的新 DAL 也需要接受 where 子句。我想使用更最新的数据访问技术和技巧,因此正在考虑使用.Net 4.0 中的实体框架。

然而,看起来我不能接受动态 where 子句而不实现一些密集的重新例程来将它们解析为巴表达式。有什么我错过的吗?我对实体框架运气不好吗?

谢谢, 马克

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.

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?

Thanks,
Marc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

农村范ル 2024-10-10 00:15:57

我想到了两个想法。

1). 动态 LINQ,它允许您将 where 运算符定义为字符串。

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

2)。使用某些内容作为 EntityFilter(请参阅代码此处< /a>),它允许您定义如下过滤器:

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

您可以将 IEntityFilter 提供给能够过滤该查询的业务方法:

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

        return filteredList.ToArray();
    }
}

Two ideas come in mind.

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). 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;

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();
    }
}
红尘作伴 2024-10-10 00:15:57

您可以使用 ExecuteStoreQuery 方法对数据库执行原始命令。
http://msdn.microsoft.com/en-us/library/ee358769.aspx

You can use ExecuteStoreQuery method to execute raw commands against the database.
http://msdn.microsoft.com/en-us/library/ee358769.aspx

薄情伤 2024-10-10 00:15:57

请在此处查看 Rick Strahl 的博客文章:http://www.west-wind.com /weblog/posts/160237.aspx。他的演示使用 Linq to SQL,但在 EF 中不会有太大不同。此方法涉及将 IQueryable 暴露给业务层。这并非没有争议,但值得考虑。

通过公开 IQueryable,您可以针对返回的集合编写 LINQ 查询。我不保证您能够使其与存储过程一起工作,但请尝试一下。

Check out Rick Strahl's blog post here: http://www.west-wind.com/weblog/posts/160237.aspx. His demo uses Linq to SQL but it wouldn't be far different in EF. This approach involves exposing the IQueryable to the business layer. It is not without controversy, but it's something to consider.

With the IQueryable exposed, you can write LINQ queries against the returned collection. I'm not promising that you'll be able to make it work with sprocs, but give it a try.

风流物 2024-10-10 00:15:57

面临同样的问题,现在 raptier 可能已经停产了,所以

我有一个线索,因为我使用的是仅支持 15 个表/视图的精简版。
这是一个捷径,

总是在服务器中获取一个虚拟测试数据库,并生成您想要的类,然后将其与您的类库解决方案合并,重建解决方案,然后继续进行到您想要的程度, ,

它很费力,但它的工作。

需要更多帮助,很高兴...

face same problem,, now the raptier discontinued might be,, well so

i have a clue,, as i am using lite version which support only 15 tables/views..
here is a short cut,,

always take a dummy test db in the server,, and generate the classes which you want, then merge it with your class library solution, rebuild the solution, and move on like that to the extent you want,,

its laborious but its work.

need more help,, with glad...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文