MongoDB使用Linq加入

问题描述 投票:0回答:1

我正在使用在C#中使用查找的mongodb joings。我的代码粘贴在下面:

        var a = db.A.AsQueryable();
        var b = db.B.AsQueryable();
        var query = from p in a
                    join n in b
                    on p.id equals n.attrid into joineddoc
                    select new
                    {
                        headline = p.headline
                    };

我不知道此代码中的问题是什么。我已将其复制并粘贴回我的项目中,但是我遇到的问题是

the type of one of the expressions in the join clause is incorrect. type inference failed in the call to 'join'.

请对此提供帮助。

谢谢!

c# database mongodb join lookup
1个回答
0
投票

使用Blueshift.EntityFrameworkCore,我有以下解决方案:

public class Document
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    [BsonRepresentation(BsonType.ObjectId)]
    public string attrid { get; set; }
}

public class Paragraph
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public string headline { get; set; }
}

[MongoDatabase("test2")]
public class DocumentContext : DbContext
{
    public DbSet<Document> Documents { get; set; }
    public DbSet<Paragraph> Paragraphs { get; set; }

    public DocumentContext()
        : this(new DbContextOptions<DocumentContext>())
    {
    }

    public DocumentContext(DbContextOptions<DocumentContext> documentContext)
        : base(documentContext)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string connectionString = "mongodb://localhost:27017";
        //optionsBuilder.UseMongoDb(connectionString);

        var mongoUrl = new MongoUrl(connectionString);
        //optionsBuilder.UseMongoDb(mongoUrl);

        MongoClientSettings settings = MongoClientSettings.FromUrl(mongoUrl);
        settings.AllowInsecureTls = true;
        //settings.SslSettings = new SslSettings
        //{
        //    EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12
        //};
        //optionsBuilder.UseMongoDb(settings);

        MongoClient mongoClient = new MongoClient(settings);
        optionsBuilder.UseMongoDb(mongoClient);
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (var db = new DocumentContext())
        {
            var joined = from p in db.Paragraphs
                join n in db.Documents
                    on p.Id equals n.attrid into joineddoc
                select new
                {
                    headline = p.headline
                };
            foreach (var row in joined)
            {
                Console.WriteLine(row.headline);
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.