简体   繁体   English

如何在 C# 中动态链接 Linq 方法?

[英]How to chain Linq methods dynamically in C#?

I working with MongoDB driver and I have the following classes:我使用 MongoDB 驱动程序,我有以下课程:

public class Transactions
{
    public ObjectId Id { get; set; }
    public int UserId { get; set; }
    public int AccountId { get; set; }
    public int SettingId { get; set; }
}
public class Account
{
    public int Id {get; set;}
    public int Name {get; set;}
}
public class User
{
    public int Id {get; set;}
    public int Name {get; set;}
}
public class Setting
{
    public int Id {get; set;}
    public int Name {get; set;}
}

And I want form this, depending of the input of user:我想形成这个,这取决于用户的输入:

var docs = collection.Aggregate()
                     .Lookup("account", "AccountId", "_id", "asAccounts")
                     .Lookup("user", "UserId", "_id", "asUsers")
                     .Lookup("setting", "SettingId", "_id", "asSettings")
                     .As<BsonDocument>()
                     .ToList();

That is, if the user just wanna the relation with account, form this:也就是说,如果用户只想与帐户建立关系,请形成:

var docs = collection.Aggregate()
                         .Lookup("account", "AccountId", "_id", "asAccounts")
                         .As<BsonDocument>()
                         .ToList();

Or if him wanna the relation with account and user:或者如果他想要与帐户和用户的关系:

var docs = collection.Aggregate()
                         .Lookup("user", "UserId", "_id", "asUsers")
                         .Lookup("setting", "SettingId", "_id", "asSettings")
                         .As<BsonDocument>()
                         .ToList();

It I trying to do is form the query depending the needs of user.我试图做的是根据用户的需要形成查询。 Just wanna know how to chain the methods in runtime.只想知道如何在运行时链接方法。

You can create your dynamic chain using some conditional tests to prepare the query:您可以使用一些条件测试来创建动态链来准备查询:

var docs = collection.Aggregate();

if ( WantRelationWithAccount )
  docs = docs.Lookup("account", "AccountId", "_id", "asAccounts")

if ( WantRelationWithUser )
  docs = docs.Lookup("user", "UserId", "_id", "asUsers")

if ( WantSettings )
  docs = docs.Lookup("setting", "SettingId", "_id", "asSettings")

var result = docs.As<BsonDocument>().ToList();

But remember that order of operations in the chain is as added.但请记住,链中的操作顺序是添加的。

Linq queries are deferred executed, that means they are only executed when used like with a loop or a any method that causes the execution like ToList . Linq 查询被延迟执行,这意味着它们仅在与循环或任何导致执行的方法(如ToList )一起使用时才会执行。

There is a linq ToLookup method but not Lookup , and it causes the query execution.有一个 linq ToLookup方法,但没有Lookup ,它会导致查询执行。

Does ToLookup forces immediate execution of a sequence ToLookup 是否强制立即执行序列

Perhaps you use extension methods provided by a special framework.也许您使用了特殊框架提供的扩展方法。

In anyway, staging execution does not prevent you from create the chain as exposed previously.无论如何,暂存执行不会阻止您创建之前公开的链。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM