Entity Framework 4 和 Linq to Entities 规范:如何编码?
我放弃了这段代码,因为它有效,但我确实需要重构为可接受的东西。它接受一组查询对象(类似于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我唯一看到类似的是:
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.
看看 LinqSpecs,它可能会满足您的需要,或者至少给您一些可以使用的想法。
据我了解,你也许可以做类似的事情:
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: