不能使用LINQ的使用嵌套类List<>有关MongoDB C# [英] Cant use Linq with nested class List<> on MongoDb C#

查看:270
本文介绍了不能使用LINQ的使用嵌套类List<>有关MongoDB C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

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>();
}



我能够存储公司在蒙戈类对象与插入()。问题是,当我搜索一个文件,并尝试使用LINQ上的列表与LT;> 项目。 。公司>()
。凡(CPY =我不断地得到一个异常

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:无法确定表达式的系列化
信息:Enumerable.Count

System.NotSupportedException: Unable to determine the serialization information for the expression: Enumerable.Count

我刚开始使用的今天蒙戈,但我认为在 LINQ 支持是比较成熟的。谁能告诉我,如果我可以做一个嵌套数组查询像我和 C# LINQ

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 ?

当我删除其中()上的任何的列表与LT;> ,也没有抛出异常。

As soon as I remove the Where() on any of the List<> , that exception isn't thrown

推荐答案

您的异常问题区域去是你的归宿之内做其中,语句。

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);



然后,您正在检查是否有任何通过调用计数和检查,如果是大于0,让您的布尔在那里设有专卖店经销商。所有这一切都与调用 IEnumerable.Any()。看看这个工程? :)

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? :)

这篇关于不能使用LINQ的使用嵌套类List&LT;&GT;有关MongoDB C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆