本文介绍了Mongodb,Linq驱动程序.如何使用变量或语句构造包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Mongo LINQ驱动程序用于C#,效果很好.

I'm using the Mongo LINQ Driver for C#, works great.

排序许多属性,但这是我无法解决的问题,它可能很简单.

Sorting a lot of properties but heres a problem I can't solve, its probably simple.

var identifierList = new []{"10", "20", "30"};
var newList = list.Where(x => identifierList.Contains(x.Identifier));

This is NOT supported ... 

所以我可以做类似的事情:

So I could do something like:

 var newList = list.Where(x => x.Identifier == "10" || x.Identifier == "20" || x.Identifier == "30");

但是由于列表是可变的...我该如何构造以上内容?还是有更好的选择?

But since the list is variable ... how do I construct the above? Or are there even better alternatives?

list的类型为IQueryable<MyCustomClass>

有关信息……这被用作许多属性的过滤器.在SQL中,我可以有一个父->子关系.但是由于我不能作为主要ID的父代,因此我需要取出所有ID,然后像这样构造它.

For information ... this is used as a filter of alot of properties. In SQL I could have a parent -> child relationship. But as I can't as the parent for the main ID I need to take all the ID's out and then construct it like this.

希望这是有道理的.如果需要的话,我会解释更多.

Hopes this makes sense. If needed I will explain more.

推荐答案

回答我自己的问题... Mongo Sharp LINQ驱动程序具有一个名为"In"的扩展方法,该扩展方法完全可以满足我的需要.

To answer my own question ... The Mongo Sharp LINQ driver has an extension method called "In" which does exactly what I need.

他们已经在1.5版中实现了它,因此我们可以使用以下旧方法: https://jira.mongodb.org/browse/CSHARP-462

They have however implemented it in 1.5 so we can use the old way like: https://jira.mongodb.org/browse/CSHARP-462

 var list = new []{"10", "10"};

 search.Where(x => list.Contains(x.Id));

但是1.5版的软件包尚未安装在nuget上.

But the version 1.5 package is not on nuget yet.

但是,这应该与mongo-csharp-driver的"In"扩展名配合使用.

However, this should work with the "In" extension that comes as a special surprise with the mongo-csharp-driver.

 search.Where(x => x.In(list));

这篇关于Mongodb,Linq驱动程序.如何使用变量或语句构造包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:01