简体   繁体   English

不能在MongoDb C#上使用Linq和嵌套类List <>

[英]Cant use Linq with nested class List<> on MongoDb C#

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() . 我可以使用Insert()Company类对象存储在Mongo中。 The problem is when I search for a document and try to use LINQ on the List<> items. 问题是当我搜索文档并尝试在List<>项目上使用LINQ时。 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 System.NotSupportedException:无法确定表达式的序列化信息:Enumerable.Count

I just started using Mongo today, but I thought the LINQ support was more mature. 我今天刚开始使用Mongo,但我认为LINQ支持更加成熟。 Can anyone tell me if I can do a nested array query like I've done with C# and LINQ ? 任何人都可以告诉我,我是否可以像C#LINQ一样进行嵌套数组查询?

As soon as I remove the Where() on any of the List<> , that exception isn't thrown 只要删除任何List<>上的Where() ,就不会抛出该异常

Going by your exception the problem area is within where you are doing Where statements. 在你的例外情况下,问题区域就在你在Where做声明。

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. 然后,您通过调用计数检查是否有any经销商存储,并检查是否超过0以使您的bool在哪里。 All of this is the same as calling IEnumerable.Any() . 所有这些都与调用IEnumerable.Any()相同。 See if this works? 看看这是否有效? :) :)

You could write you query more efficiently as 你可以更有效地写信给你

var query =  collection.AsQueryable<Company>()
                 .Where(c => c.dealers.Any(d => d.stores.Count == 1);

If the Mongo querty provider is struggling to support IList , you might find 如果Mongo querty提供商正在努力支持IList ,您可能会发现

var query =  collection.AsQueryable<Company>()
                 .Where(c => c.dealers.Any(d => d.stores.Count() == 1);

works better. 效果更好。 If so, reports of the maturity of MongoDBs query provider are exaggerated. 如果是这样,MongoDBs查询提供程序的成熟度报告就会被夸大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM