LINQ在OData中的SelectMany等价

时间:2016-05-10 23:33:37

标签: c# entity-framework entity-framework-6 odata

我有一个名为AccountAction的实体,其实体名为TransactionTypes,属于ICollectionTransactionType实体。

我在名为TransactionTypes的方法中返回与所有AccountActions相关的所有GetTransactionTypes的列表。我想将查询选项应用于返回的TransactionTypes列表。但是,到目前为止,我已经遇到了问题,因为所有查询选项都应用于AccountActions

有什么方法可以将URL中的查询选项应用到返回的TransactionTypes列表中?换句话说,我是否可以通过网址SelectMany获取与TransactionTypes相关的AccountActions继续将查询选项应用于找到的TransactionTypes }}?

以下是我正在使用的代码的摘录。

[Route(FullControllerPath + "/TransactionTypes")]
public IHttpActionResult GetTransactionTypes(ODataQueryOptions<AccountAction> queryOptions, bool addCols, int? skip, int? take)
{
    using (AccountActionManagement _accountActionManage = new AccountActionManagement(this.GenerateInformation()))
    {
        _accountActionManage.SetTraslationList("DATASTRUCT-CONFIG-ACCOUNTACTIONTRANSACTIONTYPE", language);

        // Query composition
        IQueryable<TransactionType> query = queryOptions.ApplyTo(_accountActionManage.GetTypeAsQueryable<AccountAction>())
                                                        .OfType<AccountAction>()
                                                        .SelectMany(aa => aa.TransactionTypes)
                                                        .Include(tt => tt.AccountActionForDefaultTransactionType.DefaultTransactionType);

        var queryData = query.Select(tt => new
                        {
                            Id = tt.Id,
                            Name = tt.Name,
                            Operation = tt.Operation,
                            Type = tt.Type,
                            Default = tt.AccountActionForDefaultTransactionType != null && 
                                      tt.AccountActionForDefaultTransactionType.DefaultTransactionType.Id == tt.Id,
                            Version = tt.AccountActionForDefaultTransactionType.Version
                        });
        // Get count
        int totalRows = queryData.Count();

        // Get biggest version in query
        var maxVersion = queryData.Max(i => i.Version);

        // Get data from database
        var queryResult = queryOptions.OrderBy == null 
                              ? queryData.OrderBy(i => i.Id)
                                         .Skip(skip ?? 0)
                                         .Take(take ?? totalRows)
                                         .ToList() 
                              : queryData.Skip(skip ?? 0)
                                         .Take(take ?? totalRows)
                                         .ToList();
...}}

如下图所示,AccountAction与TransactionType具有多对多关系。 AccountAction有第一个角色,TransactionType有第二个角色。

AccountActionTransactionTypeDiagram

1 个答案:

答案 0 :(得分:0)

我找到了解决此问题的方法。我意识到我没有将正确的类型传递给ApplyTo方法。现在,我将查询选项应用于IQueryableTransactionType,而不是将查询选项应用于IQueryable AccountAction个。

以下是具有所述修改的代码。此外,我所做的更改的diffchecker是here

[Route(FullControllerPath + "/TransactionTypes")]
public IHttpActionResult GetTransactionTypes(ODataQueryOptions<AccountAction> queryOptions, bool addCols, int? skip, int? take)
{
    using (AccountActionManagement _accountActionManage = new AccountActionManagement(this.GenerateInformation()))
    {
        _accountActionManage.SetTraslationList("DATASTRUCT-CONFIG-ACCOUNTACTIONTRANSACTIONTYPE", language);

        // Query composition
        IQueryable<TransactionType> query = queryOptions.ApplyTo(_accountActionManage.GetTypeAsQueryable<AccountAction()
                               .SelectMany(aa => aa.TransactionTypes)
                               .Include(aa =>      aa.AccountActionForDefaultTransactionType.DefaultTransactionType))
                               .OfType<TransactionType>();


        var queryData = query.Select(tt => new
                        {
                            Id = tt.Id,
                            Name = tt.Name,
                            Operation = tt.Operation,
                            Type = tt.Type,
                            Default = tt.AccountActionForDefaultTransactionType != null && 
                                      tt.AccountActionForDefaultTransactionType.DefaultTransactionType.Id == tt.Id,
                            Version = tt.AccountActionForDefaultTransactionType.Version
                        });
        // Get count
        int totalRows = queryData.Count();

        // Get biggest version in query
        var maxVersion = queryData.Max(i => i.Version);

        // Get data from database
        var queryResult = queryOptions.OrderBy == null 
                              ? queryData.OrderBy(i => i.Id)
                                         .Skip(skip ?? 0)
                                         .Take(take ?? totalRows)
                                         .ToList() 
                              : queryData.Skip(skip ?? 0)
                                         .Take(take ?? totalRows)
                                         .ToList();
...}}