不能做AsQueryable已对MongoDB的集合 [英] cannot do asqueryable on mongodb collection

查看:556
本文介绍了不能做AsQueryable已对MongoDB的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个型号,一个用户,并按以下团队:

I got two models, a User and a Team as below:

    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId _id { get; set; }
    [Display(Name = "Password:")]
    public string Password { get; set; }
    [Display(Name = "Confirm:")]
    public string ConfirmPassword { get; set; }
    [Display(Name = "Email:")]
    public string Email { get; set; }
    [Display(Name = "Username:")]
    public string UserName { get; set; }
    [Display(Name = "Firtname:")]
    public string Firstname { get; set; }
    [Display(Name = "Lastname:")]
    public string Lastname { get; set; }
    [Display(Name = "Country:")]
    public string Country { get; set; }
    [Display(Name = "City:")]
    public string City { get; set; }
    [Display(Name = "Birthdate:")]
    public int Birthdate { get; set; }
    public List<Team> Teams { get; set; }


   [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId TeamID { get; set; }
    public string TeamName { get; set; }
    public string UserName { get; set; }
    public int LeagueID { get; set; }

    public List<Player> Player { get; set; }

所以,我创建了一个用户,但现在我想团队添加到我的用户。 这是code我使用:

So I've created a user but now I want to add teams to my user. This is the code I'm using:

  var databaseClient = new MongoClient(Settings.Default.FantasySportsConnectionString);
  var server = databaseClient.GetServer();
  var database = server.GetDatabase("Users");
  var collection = database.GetCollection<User>("users");

  var user = collection.AsQueryable().First(o => o._id == Session["ID"]);

  user.Teams.Add(new Team { TeamID = new ObjectId(), TeamName = "Some Team" });

但是,当我这样做,我得到这些错误:

But when I do this I get these errors:

1 实例的说法:不能将'MongoDB.Driver.MongoCollection&LT; SportsFantasy_2._0.Models.User&GT;以System.Collections.IEnumerable

2:MongoDB.Driver.MongoCollection&LT; SportsFantasy_2._0.Models.User&GT;不包含定义AsQueryable已'和最佳扩展方法重载'System.Linq.Queryable.AsQueryable(System.Collections.IEnumerable)'有一些无效的参数

推荐答案

您缺少一个命名空间, MongoDB.Driver.Linq ,简单的添加,在顶部:

You are missing a namespace, MongoDB.Driver.Linq, simply add that at the top:

using MongoDB.Driver.Linq;

这是具体的方法是:

LinqExtensionMethods
{
    public static IQueryable<T> AsQueryable<T>(this MongoCollection<T> collection);
    //...
}

这篇关于不能做AsQueryable已对MongoDB的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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