2011-06-27 26 views
2

我試圖在C#中使用linq創建一個通用的「搜索引擎」。我有一個簡單的搜索引擎,其功能和外觀如下所示。使用C#動態篩選列/屬性到EF 4.1查詢

var query = "joh smi"; 
var searchTerms = query.Split(new char[] { ' ' }); 
var numberOfTerms = searchTerms.Length; 

var matches = from p in this.context.People 
       from t in searchTerms 
       where p.FirstName.Contains(t) || 
       p.LastName.Contains(t) 
       group p by p into g 
       where g.Count() == numberOfTerms 
       select g.Key; 

我希望它是更通用的,因此我可以這樣調用:

var matches = Search<Person>(dataset, query, p => p.FirstName, p => p.LastName); 

據我已經得到了作爲追隨者,但是它失敗了「的LINQ表達式節點類型「調用」在LINQ to Entities中不受支持。「 System.NotSupportedException。

static IEnumerable<T> Find<T>(IQueryable<T> items, string query, 
           params Func<T, string>[] properties) 
{ 
    var terms = query.Split(' '); 
    var numberOfParts = terms.Length; 

    foreach (var prop in properties) 
    { 
     var transformed = items.SelectMany(item => terms, 
       (item, term) => new { term, item }); 

               // crashes due to this method call 
     var filtered = transformed.Where(p => prop(p.item).Contains(p.term)); 
     items = filtered.Select(p => p.item); 
    } 

    return from i in items 
      group i by i into g 
      where g.Count() == numberOfParts 
      select g.Key; 
} 

我敢肯定這是可行的,有剛有要編譯i => i.FirstNameExpression<Func<T, bool>>的方式,但是這就是我的LINQ的專業知識結束。有沒有人有任何想法?

回答

1

您應該使用謂詞生成器來構建你的Or查詢,像:

var predicate = PredicateBuilder.False<T>(); 
foreach (var prop in properties) 
{ 
    Func<T, string> currentProp = prop; 
    predicate = predicate.Or (p => currentProp(p.item).Contains(p.term)); 
} 
var result = items.Where(predicate); 
+0

這仍然不允許LINQ到實體以擴大表達 P => currentProp(p.item )。載(p.term) – djeebus

0

考慮使用一個規範模式。檢查出this blog。具體來說,看看他開發的規格模式。這與@Variant類似,您可以在其中構建動態規範並將其傳遞到您的上下文或存儲庫。

0

原來查詢的內容只需要'擴展'。我用庫發現here來擴展表達式。我認爲這允許Linq to Entities將其轉換爲sql。你會注意到Expand被一次又一次地調用;我認爲所有這些都是必要的。無論如何,它工作。代碼遵循:

using System.Linq.Expressions; 

public static class SearchEngine<T> 
{ 
    class IandT<T> 
    { 
     public string Term { get; set; } 
     public T Item { get; set; } 
    } 

    public static IEnumerable<T> Find(
        IQueryable<T> items, 
        string query, 
        params Expression<Func<T, string>>[] properties) 
    { 
     var terms = query.Split(new char[] { ' ' }, 
           StringSplitOptions.RemoveEmptyEntries); 
     var numberOfParts = terms.Length; 

     Expression<Func<IandT<T>, bool>> falseCond = a => false; 
     Func<Expression<Func<IandT<T>, bool>>, 
      Expression<Func<IandT<T>, bool>>, 
      Expression<Func<IandT<T>, bool>>> combineOr = 
       (f, g) => (b) => f.Expand(b) || g.Expand(b); 

     var criteria = falseCond; 

     foreach (var prop in properties) 
     { 
      var currentprop = prop; 
      Expression<Func<IandT<T>, bool>> current = c => 
        currentprop.Expand(c.Item).IndexOf(c.Term) != -1; 
      criteria = combineOr(criteria.Expand(), current.Expand()); 
     } 

     return from p in items.ToExpandable() 
       from t in terms 
       where criteria.Expand(new IandT<T> { Item = p, Term = t }) 
       group p by p into g 
       where g.Count() == numberOfParts 
       select g.Key; 
    } 
} 

它可以通過下面的代碼來調用:

var matches = Search<Person>(dataset, query, p => p.FirstName, p => p.LastName);