[英]LINQ's SelectMany Equivalent in OData
I have an entity called AccountAction
that has property called TransactionTypes
that is an ICollection
of TransactionType
entities. 我有一个称为AccountAction
的实体,它具有名为TransactionTypes
属性,该属性是TransactionType
实体的ICollection
。
I return a list of all the TransactionTypes
that are related to all the AccountActions
in a method called GetTransactionTypes
. 我在名为GetTransactionTypes
的方法中返回与所有AccountActions
相关的所有TransactionTypes
的列表。 I would like to apply query options to the returned list of TransactionTypes
. 我想将查询选项应用于TransactionTypes
的返回列表。 However, so far I have hit a wall because all of the query options are applied to AccountActions
. 但是,到目前为止,我遇到了麻烦,因为所有查询选项都应用于AccountActions
。
Is there any way I can apply query options in the URL to the returned lists of TransactionTypes
? 有什么方法可以将URL中的查询选项应用于返回的TransactionTypes
列表? In other words, is there a way I can do a SelectMany
from the URL to get the TransactionTypes
related to the AccountActions
to move on to apply the query options to the found TransactionTypes
? 换句话说,是否可以通过URL执行SelectMany
以获得与AccountActions
相关的TransactionTypes
,然后继续将查询选项应用于找到的TransactionTypes
?
Below is an extract of the code that I am using. 下面是我正在使用的代码的一部分。
[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();
...}}
As seen in the diagram below, AccountAction has a many-to-many relationship to TransactionType
. 如下图所示,AccountAction与TransactionType
具有多对多关系。 AccountAction
has the first role and TransactionType
has the second role. AccountAction
具有第一个角色, TransactionType
具有第二个角色。
I found a workaround for this issue. 我找到了解决此问题的方法。 I realized that I was not passing the right type to the ApplyTo
method. 我意识到我没有将正确的类型传递给ApplyTo
方法。 Now, I apply the query options to an IQueryable
of TransactionType
s instead of applying the query options to an IQueryable
of AccountAction
s. 现在,我将查询选项应用于TransactionType
的IQueryable
,而不是将查询选项应用于AccountAction
的IQueryable
。
Below is the code with the described modification. 以下是具有描述的修改的代码。 Also, a diffchecker of the change I made is 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();
...}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.