不能使用LINQ的使用嵌套类List<>有关MongoDB C#

编程入门 行业动态 更新时间:2024-10-25 21:15:37
本文介绍了不能使用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#

本文发布于:2023-11-22 09:43:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   List   LINQ   amp   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!