gpt4 book ai didi

c# - 不能对 mongodb 集合做 asqueryable

转载 作者:太空狗 更新时间:2023-10-29 18:20:05 26 4
gpt4 key购买 nike

我有两个模型,一个用户和一个团队,如下所示:

    [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; }

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

  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" });

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

1:Instance argument: cannot convert from 'MongoDB.Driver.MongoCollection<SportsFantasy_2._0.Models.User>' to 'System.Collections.IEnumerable'

2:'MongoDB.Driver.MongoCollection<SportsFantasy_2._0.Models.User>' does not contain a definition for 'AsQueryable' and the best extension method overload 'System.Linq.Queryable.AsQueryable(System.Collections.IEnumerable)' has some invalid arguments

最佳答案

您缺少命名空间 MongoDB.Driver.Linq,只需将其添加到顶部即可:

using MongoDB.Driver.Linq;

那个具体的方法是:

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

关于c# - 不能对 mongodb 集合做 asqueryable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24174897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com