Entity Framework 4 和 Linq to Entities 规范:如何编码?

发布于 2024-10-03 10:33:07 字数 1815 浏览 3 评论 0原文

我放弃了这段代码,因为它有效,但我确实需要重构为可接受的东西。它接受一组查询对象(类似于 Productid = 3 的字符串),然后将它们添加到我的查询中。这仅适用于逻辑 AND,但我最终将需要一些不同的逻辑运算符(OR、NOT)。

-- Idea here is add the where clause to the original query and return a new one
private static IQueryable<Product> GetFilteredQuery(string condition,
    IQueryable<Product> originalQuery)
    {
        -- REPETITION 
        if( -- Regex comparison looking for "productid = 123" --)
        {   
            returnQuery = originalQuery.Where(
                    p => p.myEntity.SelectMany(q => q.subEntity) // spec expression
                          .Any(r => r.id == foundid));           
        }
        ... (one if statement for each specification, calling this several times)

我也有这个用于排序:

private static IQueryable<Product> GetOrderedQuery( IList<string> fields,
    IQueryable<Product> originalQuery)
{
    var resultQuery = originalQuery;
    bool firstTime = true;
    foreach( var field in fields)
    {   
        -- REPETITION  
        if( field == "id")
        {    if( firstTime == true)
             {   resultQuery = resultQuery.OrderBy( p => p.id);
                 firstTime = false;
             }
             else
             {   resultQuery = resultQuery.ThenBy( p => p.id);
             }
        }
        ... (one for each field to order by)
    }

那么我如何将每个重复封装到一个规范对象中,在该对象中我可以以某种方式将这个规范集合附加到我的原始查询(包括订单表达式)?这是在 Linq to Entities、Entity Framework 4 和 C# 的保护下。

做这样的事情真是太好了,这本质上就是上面所做的事情。

var originalQuery = ...;
foreach( var spec in mySpecs)
{    originalQuery = spec(originalQuery);  //adds all the where clauses
}

originalQuery = orderSpec( originalQuery); // adds all the order fields

网站链接、示例代码肯定会受到赞赏。

I threw down this code because it worked, but I really need to refactor to something acceptable. It accepts a set of query objects (strings that look like productid = 3) then adds them to my query. This only works for logical AND, but I will eventually need a few different logical operators (OR, NOT).

-- Idea here is add the where clause to the original query and return a new one
private static IQueryable<Product> GetFilteredQuery(string condition,
    IQueryable<Product> originalQuery)
    {
        -- REPETITION 
        if( -- Regex comparison looking for "productid = 123" --)
        {   
            returnQuery = originalQuery.Where(
                    p => p.myEntity.SelectMany(q => q.subEntity) // spec expression
                          .Any(r => r.id == foundid));           
        }
        ... (one if statement for each specification, calling this several times)

I also have this for Ordering:

private static IQueryable<Product> GetOrderedQuery( IList<string> fields,
    IQueryable<Product> originalQuery)
{
    var resultQuery = originalQuery;
    bool firstTime = true;
    foreach( var field in fields)
    {   
        -- REPETITION  
        if( field == "id")
        {    if( firstTime == true)
             {   resultQuery = resultQuery.OrderBy( p => p.id);
                 firstTime = false;
             }
             else
             {   resultQuery = resultQuery.ThenBy( p => p.id);
             }
        }
        ... (one for each field to order by)
    }

So how could I encapsulate each repetition in to a specification object where I can somehow attach this collection of specifications to my original query, including the order expressions? This is under the Linq to Entities, Entity Framework 4, and C# umbrella.

It would be really nice to do something like this, which is essentially what the above does.

var originalQuery = ...;
foreach( var spec in mySpecs)
{    originalQuery = spec(originalQuery);  //adds all the where clauses
}

originalQuery = orderSpec( originalQuery); // adds all the order fields

Links to websites, example code, would surely be appreciated.

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

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

发布评论

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

评论(2

一张白纸 2024-10-10 10:33:07

我唯一看到类似的是:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query -library.aspx

但这并不是 EF 4 特有的。为什么不将查询转换为实体 SQL?

您可以将查询创建为字符串,并将这些术语附加到该 SQL 查询中。

HTH。

The only thing I saw something similar was this:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

But that's not specific to EF 4. Why not convert the query to entity SQL?

You can create the query as a string, and append these terms to that SQL query.

HTH.

执笔绘流年 2024-10-10 10:33:07

看看 LinqSpecs,它可能会满足您的需要,或者至少给您一些可以使用的想法。

据我了解,你也许可以做类似的事情:

var originalSpec = ...;
var composedSpec = originalSpec;

foreach(var spec in mySpecs)
{    
    composedSpec &&= spec;  //adds all the where clauses
}

composedSpec &&= orderSpec; // adds all the order fields

Have a look at LinqSpecs, it might do what you need, or at least give you some ideas to work with.

From what I understand you might be able to do something like:

var originalSpec = ...;
var composedSpec = originalSpec;

foreach(var spec in mySpecs)
{    
    composedSpec &&= spec;  //adds all the where clauses
}

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