2015-07-21 71 views
0

我想實現下面的RavenDB查詢的邏輯,但收到難度創建適當RavenDB查詢

System.NotSupportedException: Could not understand expression

相關scores.Any表達。我明白這是爲什麼,但我很難提出一個工作方案。

public IRavenQueryable<Person> Apply(IRavenQueryable<Person> query) 
{ 
    var scores = new List<string>(); 
    if (_a) scores.Add("A"); 
    if (_b) scores.Add("B"); 
    if (_c) scores.Add("C"); 
    if (_u) 
    { 
     scores.Add(""); 
     scores.Add(" "); 
     scores.Add("\t"); 
     scores.Add(null); 
    } 

    return from p in query 
      where scores.Any(score => score == p.Score) 
      select p; 
} 

回答

1

的訣竅是,ravendb LINQ提供程序是不是名單​​上的操作,以便scores.Any()使零意義 - 它編譯,但你可以看到它死在運行時。

訣竅是將字段反轉一下,並詢問p.Score是否在分數組中,如果我正確記得。

0

如果我們修改您的示例使用IDocumentQuery,它應該工作:

public IDocumentQuery<Person> Apply(IDocumentQuery<Person> query) 
{ 
    var scores = new List<string>(); 
    if (_a) scores.Add("A"); 
    if (_b) scores.Add("B"); 
    if (_c) scores.Add("C"); 
    if (_u) 
    { 
     scores.Add(""); 
     scores.Add(" "); 
     scores.Add("\t"); 
     scores.Add(null); 
    } 

    return query.AndAlso().WhereIn(x => x.Score, scores); 
} 

您最初的文檔的查詢可能類似於:

var myQuery = RavenSession.Advanced.DocumentQuery<Person>();