我拋棄了這段代碼,因爲它工作,但我真的需要重構一些可以接受的東西。它接受一組查詢對象(看起來像productid = 3的字符串),然後將它們添加到我的查詢中。這隻適用於邏輯AND,但我最終需要一些不同的邏輯運算符(OR,NOT)。實體框架4和Linq to Entities規範:如何對其進行編碼?
-- 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
鏈接到網站,示例代碼,肯定會被讚賞。
我是這裏唯一一個缺少這些方法背後的意義呢?讓調用者自己將LINQ調用鏈接在一起,而不是試圖過度設計某些東西來隱藏它們,不是簡單嗎? – 2010-11-19 02:22:06
這些字段來自客戶端。客戶端是一個來回發送JSON的Web瀏覽器。我將需要遍歷客戶端提供的每一個實現它。 – 2010-11-19 02:27:54
如何將JSON反序列化爲.NET對象(讓它稱爲搜索條件),然後將Linq構建爲實體查詢衆所周知的搜索條件結構? – 2010-11-19 10:12:18