如何在C#中创建派生类

时间:2015-06-15 12:36:23

标签: mongodb mongodb-.net-driver

我有一个名为Entry的基类:

public class AccountEntry : Entry
{
    public Account Account { get; set; }
}

使用名为AccountEntry的派生类作为示例

var filter = Builders<AccountEntry>.Filter.Where(x => x.Id = id);            
await context.Entries.UpdateOneAsync(filter, new AccountEntry()
{                                                
    Account = debitAccount,                
}, new UpdateOptions() { IsUpsert = true });

当我尝试进行更新时,编译时错误显示,表示AccountEntry无法转换为条目

    import cv2
    import numpy as np
    import glob
    import json, io
    from matplotlib import pyplot as plt
    from PIL import Image

    img = cv2.imread(fname, 0);

    i = Image.fromarray(self.__imgremapped_bw)

    pixels = i.load() # this is not a list

    self.__pointsData = [];

    find = 0

    for y in range(self.__top,self.__bottom):
        row_averages = []
        for x in range(self.__top,self.__bottom):
            cur_pixel = pixels[x, y]
            if cur_pixel >= self.__thresholdColor:
                row_averages.append(x)
                find = 1
            elif find == 1:
                pointSum = 0
                for idx, val in enumerate(row_averages):
                    pointSum += row_averages[idx];

                xf = pointSum/len(row_averages)
                # 0.5 correzione pixel al centro
                self.__pointsData.append([[y+0.5,xf+0.5]])
                row_averages = []
                find = 0

    #self.__drawPoint(self.__imgremapped_bw)

    return self.__pointsData

插入派生类不会导致同样的问题。

1 个答案:

答案 0 :(得分:0)

Your filter needs to be of type Entry, not AccountEntry.

var filter = Builders<Entry>.Filter.Where(x => x.Id == id);

This is because filter is not covariant (I think that's the right one). So, the compiler can't just use a FilterDefinitionBuilder in place of a FilterDefinitionBuilder.

We have a ticket open to better handle derived types: https://jira.mongodb.org/browse/CSHARP-1194.