I have the following classes:
public class Company
{
[BsonId]
public string dealerId = null;
public List<Dealer> dealers = new List<Dealer>();
}
public class Dealer
{
public string dId = null;
public int dIndex = -1;
public List<AutoStore> stores = new List<AutoStore>();
}
public class AutoStore
{
public string type = null;
public Dictionary<string, object> data = new Dictionary<string, object>();
}
I am able to store the Company class objects in Mongo with Insert(). The problem is when I search for a document and try to use LINQ on the List<> items. I constantly get an exception .
var query = collection.AsQueryable<Company>()
.Where(cpy =>
cpy.dealers.Where(dlr =>
dlr.stores.Count == 1).Count() > 0) ;
Running this code I get:
System.NotSupportedException: Unable to determine the serialization information for the expression: Enumerable.Count
I just started using Mongo today, but I thought the LINQ support was more mature. Can anyone tell me if I can do a nested array query like I've done with C# andLINQ ?
As soon as I remove the Where() on any of the List<> , that exception isn't thrown
Counttwice? Try to usecpy.Dealers.Any(dlr => dlr.stores.Count ==1)if all you want to do is see if the dealer has any stores (that's what I judge from the code).Count() > 0or.Count(lambdaFunc) > 0, always use.Any()respectively.Any(lambdaFunc). The reason is that to determine the count, it will have to iterate the entire source to the end, while to determine if there isAny, it only has to iterate until it finds the first element! (AndAnyis even easier to type thanCountand> 0.).Count() == nfor some positive integern, it might perform better to say.Take(n + 1).Count() == n. And so on.