1

我无法从 MongoDB 的内部数组中获取数据。我得到错误:

BsonSerializationException: No matching creator found.

这是我在 MongoDB、查询和环境中的数据示例。

{
        "Id" : 1,
        "compName" : "Samsung Electronics Co.",
        "symbol" : "005930-KRX",
        "analyst" : [
                {
                        "analystId" : 4,
                        "analystInit" : "SJ",
                        "analystFName" : "Steve",
                        "analystLName" : "Jobs",
                        "analystEmail" : "steve.jobs@apple.com"
                }
        ],
        "associates" : [
                {
                        "analystId" : 7,
                        "analystInit" : "BG",
                        "analystFName" : "Bill",
                        "analystLName" : "Gates",
                        "analystEmail" : "bill.gates@microsoft.com"
                },
                {
                        "analystId" : 10,
                        "analystInit" : "MJ",
                        "analystFName" : "Michael",
                        "analystLName" : "Jordan",
                        "analystEmail" : "michael.jordan@nba.com"
                }
        ],
        "gics" : "75301020",
        "cusip" : null
}

POCO

[BsonIgnoreExtraElements]
class CompanyClass
{
    #region [ Properties ]
    [BsonElement("issuerId")]
    public Int32 IssuerId { get; set; }
    [BsonElement("compName")]
    public string CompName { get; set; }
    [BsonElement("symbol")]
    public string Symbol { get; set; }
    [BsonElement("analyst")]
    public IEnumerable<AnalystClass> Analyst { get; set; }
    [BsonElement("associates")]
    public IEnumerable<AnalystClass> Associates { get; set; }
    [BsonElement("gics")]
    public string Gics { get; set; }
    [BsonElement("cusip")]
    public string Cusip { get; set; }
    #endregion
}
[BsonIgnoreExtraElements]
class AnalystClass
{
    #region [ Properties ]
    [BsonElement("init")]
    public string ResAnInit { get; set; }
    [BsonElement("firstName")]
    public string ResAnFirstName { get; set; }
    [BsonElement("lastName")]
    public string ResAnLastName { get; set; }
    [BsonElement("email")]
    public string ResAnEmail { get; set; }
    #endregion
}

询问:

compCollection = GetMonDB("Companies").GetCollection<CompanyClass>("coverage");
var comps = compCollection.AsQueryable()
.Select(c => new { 
    CompName = c.CompName,
    Symbol = c.Symbol,
    ResAnsFName = c.Analyst.Select(x => x.ResAnFirstName)  <-- Problem line
    CUSIP = c.Cusip
});

我想得到这个:

在此处输入图像描述

我的环境:

C# MongoDB.Driver = 2.12
MongoDB = 4.2
Windows = Win10

看来我错过了一些明显的东西。我究竟做错了什么?

4

2 回答 2

1

只需像这样进行投影:

var comps = await compCollection
    .AsQueryable()
    .Select(c => new
    {
        ...
        ResAnsFName = c.Analyst.first().ResAnFirstName
        ...
    })
    .ToListAsync();
于 2021-03-25T15:37:58.103 回答
0

ResAns 属性在 CompanyClass 类中不存在,因此您不能使用该属性。BsonElements 也与您的 MongoDB 数据不匹配。Select 应该足以执行 Linq,不需要 FirstorDefault 或 ToList()。

 ResAnsFName = c.Analyst.Select(x => x.ResAnFirstName)

更新

布森:

{ 
  "_id" : ObjectId("59ce6b34f48f171624840b05"), 
  "name" : "Nikola", 
  "blog" : "rubikscode.net", 
  "age" : 30, 
  "location" : "Beograd" 
}

C# 强类型对象:

    public class User
    {
        [BsonId]
        public ObjectId Id { get; set; }
        [BsonElement("name")]
        public string Name { get; set; }
        [BsonElement("blog")]
        public string Blog { get; set; }
        [BsonElement("age")]
        public int Age { get; set; }
        [BsonElement("location")]
        public string Location { get; set; }
    }
于 2021-03-25T15:48:07.493 回答