我正在尝试根据一个字符串列表获取一组Master-Detail记录,这些字符串需要与每个主节点的每个细节的参考编号列匹配。因此,例如,我将此作为字符串列表:
string[] listToFilterFor = new [] { "2729113", "2732623", "2734483", "2735355", "2752260" };
安装DAL功能进行过滤:
public async Task<IQueryable<BILL_INFO>> GetBills(IDictionary<string, object> filterCriteria, string operationGuid)
{
var callerInfo = Shared.CommonAcross.Helper.GetCaller();
Logger.Info($"{LayerName} -> {callerInfo.MethodName} -> Started");
try
{
IList<BILL_INFO> intermResult;
using (var context = new FinanceConnection())
{
var result = context.BILL_INFOS
.Include(i => i.MASTER_ACCOUNT)
.Include(i => i.MASTER_PAY_MODE)
.Include(i => i.MASTER_BANK)
.Include(i => i.MASTER_CREDIT_CARD_TYPE)
.Include(i => i.MASTER_EDIRHAM_CARD_TYPE);
if (filterCriteria != null && filterCriteria.Any())
{
#region Keys
var billNumberKey = "BillNumber";
var cashierNumberKey = "AssignedCashiers";
var payModeIdKey = "PayModeId";
var depositIdKey = "DepositId";
var dateFromKey = "DateFrom";
var dateToKey = "DateTo";
var accountsKey = "Account";
var accountGroupsKey = "AccountGroups";
var referenceNumber = "ReferenceNumber";
var referenceNumbers = "ReferenceNumbers";
#endregion
if (filterCriteria.ContainsKey(billNumberKey) && filterCriteria.TryGetValue(billNumberKey, out var actualFilterBillNumber))
result = result.Where(where => where.BILL_NUMBER.Contains(actualFilterBillNumber.ToString()));
if (filterCriteria.ContainsKey(referenceNumbers) && filterCriteria.TryGetValue(referenceNumbers, out var actualReferenceNumbers))
{
result = result.Include(i => i.BILL_INFO_DETAIL);
result = result.Where(where => where.BILL_INFO_DETAIL.Any(p=>p.));
}
#region From/To Dates
DateTime? tempDateFrom = null;
DateTime? tempDateTo = null;
if (filterCriteria.ContainsKey(dateFromKey) && filterCriteria.TryGetValue(dateFromKey, out var actualDateFrom))
{
tempDateFrom = ((DateTime?)actualDateFrom)?.Date;
}
if (filterCriteria.ContainsKey(dateToKey) && filterCriteria.TryGetValue(dateToKey, out var actualDateTo))
{
tempDateTo = ((DateTime?)actualDateTo)?.Date.AddDays(1).AddMilliseconds(-1);
}
if (tempDateFrom.HasValue && tempDateTo.HasValue)
{
result = result.Where(where => where.BILL_DATE != null && where.BILL_DATE >= tempDateFrom && where.BILL_DATE <= tempDateTo);
}
else if (tempDateFrom.HasValue && !tempDateTo.HasValue)
{
result = result.Where(where => where.BILL_DATE != null && where.BILL_DATE >= tempDateFrom && where.BILL_DATE <= tempDateFrom);
}
else if (!tempDateFrom.HasValue && tempDateTo.HasValue)
{
result = result.Where(where => where.BILL_DATE != null && where.BILL_DATE >= tempDateTo.Value.Date && where.BILL_DATE <= tempDateTo);
}
#endregion
}
intermResult = await result.OrderByDescending(o => o.BILL_DATE).Take(10000).ToListAsync();
}
Logger.Info($"{LayerName} -> {callerInfo.MethodName} -> Returning");
return intermResult.AsQueryable();
}
catch (Exception exp)
{
Logger.Error($"{LayerName} -> {callerInfo.MethodName} -> Exception [{exp.Message}]", exp);
throw;
}
}
任务:
我需要浏览主记录(BILL_INFOS
)并查看每个主人的详细记录(BILL_INFO_DETAILS
)并尝试将 AnyOf listToFilterFor与BILL_INFO_DETAIL.REFERENCE_NUMBER匹配< / p>
答案 0 :(得分:0)
如果我理解这是正确的,如果我没有,我道歉。你能使用包含声明吗?
BILL_INFO.where(x =&gt; listToFilterFor.Includes(x.BILL_INFO_DETAILS.REFERENCE_NUMBER)
答案 1 :(得分:0)
我最终对此进行了排序,因为这里涉及复合主键,并且不可能使用正确的元组。我通过这个问题找到了解决方案:Cannot create a relation between two tables with three primary keys
我能够提取一个子集。