質問

i have following problem: there is my interface:

 public interface IObjectWithID
{
    ObjectId Id { get; set; }
}

my object:

public class Order : IObjectWithID
{
    [BsonId]
    public ObjectId Id
    {
        get;
        set;
    }

    private Customer customer;

    public Customer Customer
    {
        get { return customer; }
        set { customer = value; }
    }

    public Employees employee;

    private Employees Employee
    {
        get { return employee; }
        set { employee = value; }
    }

    public IList<Product> Products = new List<Product>();

    public Order(Customer customer, Employees employee, params Product[] product)
    {

        this.customer = customer;
        this.employee = employee;
        TabelaPosredniaKlientOrder.Tabela.Add(new ObiektPosredniKlientOrder(this.Id, Customer.Id));
        TabelaPosredniaPracownikOrder.Tabela.Add(new ObiektPosredniPracownikOrder(this.Id, Employee.Id));

    }

}

there is problematic function

 public static IObjectWithID FindById<T>(MongoCursor cursor, ObjectId id) where T: IObjectWithID
    {
        var query = Query<IObjectWithID>.Where(e => e.Id == id);
        var item = cursor.Collection.FindOneAs<IObjectWithID>(query);

        return item;
    }

then is following code at startup up:

//database initialization code
// collection initialization code

var neworder = new order(param1, param2, param3);
collection.Inser(neworder); //everything fine

var item = FindByID<Order>(cursor, neworder.Id); //cursor is initializated

then it says "Additional information: Unable to determine the serialization information for the expression: e.Id." in function FindById in line

var query = Query<IObjectWithID>.Where(e => e.Id == id);

but when i switch this into:

var query = Query<Order>.Where(e => e.Id == id);
var item = cursor.Collection.FindOneAs<Order>(query);

it works fine.

The problem is dont want to have multiply if statement in this function, i want to use nice, clean interface.

Thx in advance!

役に立ちましたか?

解決

Swap IObjectWithID with T:

 public static IObjectWithID FindById<T>(MongoCursor cursor, ObjectId id) where T: IObjectWithID
    {
        var query = Query<T>.Where(e => e.Id == id);
        var item = cursor.Collection.FindOneAs<T>(query);

        return item;
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top