Cant use Linq with nested class List<> on MongoDb C#
c#, linq, mongodb
Solution
Going by your exception the problem area is within where you are doing `Where` statements.
As I said in my comment. Try to do:
var v = collection.AsQueryable<Company>().Where(cpy => cpy.Dealers.Any(dlr => dlr.Stores.Count == 1));
You are currently doing something like:
var dealers = collection.AsQueryable<Company>().Select(cpy => cpy.Dealers);
var dealersWithStores = dealers.Where(dealer => dealer.Stores.Count == 1);
You are then checking if there are `any` dealers with stores by calling count and checking if that is more than 0 to get your `bool` in the where. All of this is the same as calling `IEnumerable.Any()`. See if this works? :)
Problem
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#` and`LINQ` ? As soon as I remove the `Where()` on any of the `List<>` , that exception isn't thrown