0

我拋棄了這段代碼,因爲它工作,但我真的需要重構一些可以接受的東西。它接受一組查詢對象(看起來像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 

鏈接到網站,示例代碼,肯定會被讚賞。

+0

我是這裏唯一一個缺少這些方法背後的意義呢?讓調用者自己將LINQ調用鏈接在一起,而不是試圖過度設計某些東西來隱藏它們,不是簡單嗎? – 2010-11-19 02:22:06

+0

這些字段來自客戶端。客戶端是一個來回發送JSON的Web瀏覽器。我將需要遍歷客戶端提供的每一個實現它。 – 2010-11-19 02:27:54

+0

如何將JSON反序列化爲.NET對象(讓它稱爲搜索條件),然後將Linq構建爲實體查詢衆所周知的搜索條件結構? – 2010-11-19 10:12:18

回答

1

我看到類似的東西的唯一的事情是這樣的:

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。

+0

只有動態SQL的問題是它沒有強類型。規範模式將是強類型的,使用Lambda表達式來表示參與。 p => p.name – 2010-11-30 00:06:56

+0

對於動態SQL,我同意,您可以使用Cast 轉換爲適當的類型。實體SQL可以工作。 – 2010-12-03 13:27:40

1

看看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