[英]ContainsAny doesn't return any result
我希望能夠找到一個包含arrayOfArraysOfIds的TheType,該數組具有我提供的列表的任何元素。 問題是當我在下面進行查詢時,它不返回任何內容。 什么是正確的格式或應該使用什么功能?
public class TheType
{
public IList<IList<string>> arrayOfArraysOfIds;
}
theCollection.AsQueryable<theType>().
Where(t => t.arrayOfArraysOfIds.ContainsAny(listOfIds));
所述listOfIds是類型IEnumerable<IList<string>>
所要求的ContainsAny函數和如所描述的它是從一個列表構造這里 。
問題是即使數據庫中有應返回的元素,查詢也不會返回任何內容。
可以獲取更多控制權以及了解為什么未獲取任何值的一種方法是顯式定義自己的ContainsAny方法,以便可以使用調試器。 我發現這種做法雖然不如使用復雜的LINQ命令那么巧妙,但卻為我省下了很多麻煩。
public class TheType
{
public IList<IList<string>> arrayOfArraysOfIds;
public bool ContainsAny(IList<string> listOfIds)
{
return arrayOfArraysOfIds.Contains(listOfIds);
}
}
theCollection.AsQueryable<TheType>().Where(t => t.ContainsAny(listOfIds));
解決方案是對MongoDB進行查詢,而不要輕而易舉地解釋它,因為該查詢直接受MongoDB支持。
theCollection.Find(Query.ElemMatch("listOfIds", Query.In("$elemMatch", new BsonArray(listOfIds)))).AsQueryable();
嘗試這個:
theCollection.AsQueryable <TheType>()其中(T => t.arrayOfArraysOfIds.Contains(listOfIds));
...其中listOfIds被定義為通用列表
IList <string> listOfIds =新的List <string>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.