MongoDB的C#选择特定的字段​​点符号 [英] mongodb c# select specific field dot notation

查看:156
本文介绍了MongoDB的C#选择特定的字段​​点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另外我的previous问题:
MongoDB的C#选择特定的领域

In addition for my previous question: mongodb c# select specific field.

我在写的通用的选择特定的字段​​的方法。

I'm writing a generic method for selecting a specific field.

的要求是:


  1. 场可以是任何类型的

  2. 返回类型为T

  3. 字段可以是一个子领域内

  4. 字段可以是内部数组项 - 在这种情况下其确定在数组中选择所有项目的特定领域

有关短裤,即时寻找选择/点符号的能力。
例如:

for shorts, im looking for the "select" / dot notation capability. for example:

想要的方式:

T GetFieldValue<T>(string id, string fieldName)

文件:

persons
{
  "id": "avi"
  "Freinds" : [
    {
      "Name" : "joni",
      "age" : "33"
    },
    {
      "Name" : "daniel",
      "age" : "27"
    }]
}

的目标是调用这样的方法:

The goal is to call the method like this:

string[] myFriends = GetFieldValue<string[]>("avi", "Freinds.Name");
myFriends == ["joni","daniel"]

据我所知,使用投影前pression 与拉姆达没有很好的在数组项,
我想更多的点号办法。

as far as i know, using projection expression with lambda is no good for items in array, I was thinking more dot notation way.

注:
我使用的是新的C#驱动程序的(2.0)

非常感谢。

推荐答案

我不明白,在字符串唐记号好办法,因为它有更多的问题与收藏比通用的方法:

I don't see good approach with don notation in string, because it has more issues with collections than generic approach:

例如 Persion.Friends.Name


  1. 哪些元素是数组中的这条产业链?

  2. 您应该申请集合元素显式转换(可能发生的错误的)

泛型方法是支持更可靠,使用:

Generic methods are more reliable in support and using:

var friends = await GetFieldValue<Person, Friend[]>("avi", x => x.Friends);
var names = friends.Select(x=>x.Name).ToArray();

这篇关于MongoDB的C#选择特定的字段​​点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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