Translate Queryable back to IMongoQuery

前端 未结 2 1675
时光说笑
时光说笑 2020-12-05 08:52

Consider the following code

var q = from e in myCollection.AsQueryable() where e.Name == \"test\" select e;

The actual query

2条回答
  •  鱼传尺愫
    2020-12-05 09:27

    A quick extension based on Robert Stam's answer:

    public static IMongoQuery ToMongoQuery(this IQueryable linqQuery)
    {
        var mongoQuery = ((MongoQueryable)linqQuery).GetMongoQuery();
        return mongoQuery;
    }
    public static WriteConcernResult Delete(this MongoCollection col,   IQueryable linqQuery)
    {
         return col.Remove(linqQuery.ToMongoQuery());
    }
    public static WriteConcernResult Delete(this MongoCollection col, Expression> predicate)
    {
        return col.Remove(col.AsQueryable().Where(predicate).ToMongoQuery());
    }
    

    example:

    myCollection.Remove(myCollection.AsQueryable().Where(x => x.Id == id).ToMongoQuery());
    myCollection.Delete(myCollection.AsQueryable().Where(x => x.Id == id));
    myCollection.Delete(x => x.Id == id);
    

提交回复
热议问题