我如何使用LINQ查询嵌套动态BsonDocuments? [英] How do I use Linq to query nested dynamic BsonDocuments?

查看:247
本文介绍了我如何使用LINQ查询嵌套动态BsonDocuments?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类Foo 
{
公众的ObjectId _id {搞定;组; }
公共BsonDocument性能得到{;组; }
}

公共无效FindFoos()
{
VAR的客户=新MongoClient(ConfigurationManager.ConnectionStrings [的MongoDB]的ConnectionString);
VAR服务器= client.GetServer();
变种DB = server.GetDatabase(FooBar的);
// VAR收集= db.GetCollection< GeoJsonFeature< GeoJson2DGeographicCoordinates>>(段);
变种集合= db.GetCollection<富>(FOOS);



collection.Insert(新富
{
性能=新BsonDocument {
{富,foo1},
{酒吧,BAR1}
}
});
collection.Insert(新富
{
性能=新BsonDocument {
{富,foo2的},
{酒吧,BAR2 }
}
});


VAR的查询=查询<富>。凡(富=方式> foo.properties.AsQueryable()任何(财产= GT; property.Name ==富与放大器; &安培; property.Value.AsString ==foo1));

VAR的结果= collection.Find(查询)。首先();
}



调用以下异常FindFoos结果:



不支持的where子句:Queryable.Any(Queryable.AsQueryable(foo.properties),(BsonElement属性)=>((property.Name ==富)及及(property.Value。 。AsString ==foo1)))



说明:在当前Web请求的执行过程中发生未处理的异常。请检查堆栈跟踪有关该错误它起源于代码的更多信息和。



异常详细信息:System.ArgumentException:不支持的where子句:Queryable.Any(Queryable.AsQueryable(foo.properties),(BsonElement属性)=>((property.Name ==富)及及(property.Value.AsString ==foo1)))



在该行:

  VAR的查询=查询<富>。凡(富=> foo.properties.AsQueryable()任何(财产=方式> property.Name = =富与功放;&安培; property.Value.AsString ==foo1)); 



我可以在MongoDB的外壳那样容易做到这一点查询:

  db.Foos.find({'properties.Foo':'foo1'}); 



什么是做这个查询使用LINQ?


的正确方法< DIV CLASS =h2_lin>解决方案

尝试LINQ的的SelectMany()方法。它被用来压平嵌套集合。而是使用嵌套的for循环中,我们可以做更多的'LINQ'的方式任务



防爆如下 -

 主M1 =新硕士(){名称=A,lstObj =新的List<&OBJ GT; {新物镜{I = 1,S =Visual C ++},新物镜{I = 1,S =C#},新物镜{I = 1,S =Java的}}}; 

主设备M2 =新硕士(){名称=A,lstObj =新的List<&OBJ GT; {新的OBJ {I = 4,S =PHP},新的OBJ {I = 5,S =红宝石},新的OBJ {I = 6,S =Perl的}}};

名单,LT;法师> lstMasters =新的List<硕士> {M1,M2};
VAR的结果= lstMasters.SelectMany(M = GT; m.lstObj)。凡(O = GT; o.s ==PHP);



只是替换类的 BsonDocument


public class Foo
{
  public ObjectId _id { get; set; }
  public BsonDocument properties { get; set; }
}

public void FindFoos()
{
  var client = new MongoClient(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString);
  var server = client.GetServer();
  var db = server.GetDatabase("FooBar");
  //var collection = db.GetCollection<GeoJsonFeature<GeoJson2DGeographicCoordinates>>("Sections");
  var collection = db.GetCollection<Foo>("Foos");



  collection.Insert(new Foo
  {
    properties = new BsonDocument{
                     {"Foo" , "foo1"},
                     {"Bar" , "bar1"}
                   }
  });
  collection.Insert(new Foo
  {
    properties = new BsonDocument{
                     {"Foo" , "foo2"},
                     {"Bar" , "bar2"}
                   }
  });


  var query = Query<Foo>.Where(foo => foo.properties.AsQueryable().Any(property => property.Name == "Foo" && property.Value.AsString == "foo1"));

  var result = collection.Find(query).First();
}  

calling FindFoos results in the following exception:

Unsupported where clause: Queryable.Any(Queryable.AsQueryable(foo.properties), (BsonElement property) => ((property.Name == "Foo") && (property.Value.AsString == "foo1"))).

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Unsupported where clause: Queryable.Any(Queryable.AsQueryable(foo.properties), (BsonElement property) => ((property.Name == "Foo") && (property.Value.AsString == "foo1"))).

at the line:

var query = Query<Foo>.Where(foo => foo.properties.AsQueryable().Any(property => property.Name == "Foo" && property.Value.AsString == "foo1"));

I can do this query easily in the mongodb shell as:

db.Foos.find( {'properties.Foo' : 'foo1'} );

What is the correct way to do this query with Linq?

解决方案

Try LINQ's SelectMany() method. It is used to flatten nested collections. Instead of using nested for loops, we can do the task in a more 'LINQ' way.

Ex given below -

Master m1 = new Master() { name = "A", lstObj = new List<obj> { new obj { i = 1, s = "C++" }, new obj { i = 1, s = "C#" }, new obj { i = 1, s = "Java" } } };

Master m2 = new Master() { name = "A", lstObj = new List<obj> { new obj { i = 4, s = "PHP" }, new obj { i = 5, s = "Ruby" }, new obj { i = 6, s = "Perl" } } };

List<Master> lstMasters = new List<Master> { m1, m2 };
var result = lstMasters.SelectMany(m => m.lstObj).Where(o => o.s == "PHP");

Just replace the Master class with your BsonDocument.

这篇关于我如何使用LINQ查询嵌套动态BsonDocuments?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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