我有这个LINQ查询。
var sumOfData = fullData.AsEnumerable().GroupBy(x => new { x.transactionId, x.checkId }).
Where(p => (p.Key.checkId == variable.checkId) && (p.Key.transactionId == variable.transactionId))
.Select(y => new
{
checkId = variable.checkId,
transactionId = variable.transactionId,
SumAppliedAmt = y.Sum(z => z.appliedAmount),
SumCheckAmt = y.Sum(x => x.checkAmount)
});
如何访问sumOfData
内的数据?
访问sumOfData.checkId
或sumOfData.transactionId
需要执行哪些操作?
我想在不使用foreach循环的情况下访问它。对不起,我忘了添加那部分。我已经使用foreach (var data in fullDataQuery)
访问一个LINQ并将foreach置于foreach中将是性能密集型的{if(data.invoiceBalance ==)}
也可以直接更新到
var fullDataQuery = remitFile.AsEnumerable().Select(
remitInfofullData => new
{
checkId = remitInfofullData.Field<int>("checkId"),
depositId = remitInfofullData.Field<int>("depositId"),
appliedAmount = remitInfofullData.Field<decimal>("appliedAmount"),
checkAmount = remitInfofullData.Field<decimal>("checkAmount"),
invoiceBalance = remitInfofullData.Field<decimal>("invoiceBalance"),
sequenceId = remitInfofullData.Field<int>("sequenceId"),
remittanceId = remitInfofullData.Field<int>("remittanceId"),
fileId = remitInfofullData.Field<int>("fileId"),
transactionId = remitInfofullData.Field<int>("transactionId"),
}).Where(p => (p.checkId == variable.checkId) && (p.transactionId == variable.transactionId)); using var sumOfData = fullData.AsEnumerable().GroupBy(x => new { x.transactionId, x.checkId }).
Where(p => (p.Key.checkId == variable.checkId) && (p.Key.transactionId == variable.transactionId))
.Select(y => new
{
checkId = variable.checkId,
transactionId = variable.transactionId,
SumAppliedAmt = y.Sum(z => z.appliedAmount),
SumCheckAmt = y.Sum(x => x.checkAmount)
}); the sumAppliedAmount value
答案 0 :(得分:2)
您可以像访问任何其他集合一样访问它。例如,您可以使用foreach
:
foreach (var item in sumOfData)
{
//access properties on item by using item.checkId,
//item.transactionId, item.SumAppliedAmt, item.SumCheckAmt
//do your stuff
}
要在没有foreach
的情况下访问,您可以使用LINQ来执行检查金额最高的项目:
var highestCheckAmtItem = sumOfData.OrderByDescending(s => s.SumCheckAmt).First();
答案 1 :(得分:1)
例如,您可以使用foreach
枚举查询:
foreach (var x in sumOfData)
{
var transactioID = x.transactionId;
var checkId = x.checkId;
var sumAppliedAmt = x.SumAppliedAmt;
var sumCheckAmt = x.SumCheckAmt;
}
或您使用ToList
创建List<YourAnonymousType>
。但是,由于您只能在此方法中使用此匿名类型,因此如果要在不同位置使用它,则应使用真实类。
答案 2 :(得分:1)
sumOfData
是一个集合,而不是单个值。你可以这样做:
foreach (var element in sumOfData)
{
Console.Write("CheckID is "+ element.checkId);
Console.Write("transactionId is "+ element.transactionId);
Console.Write("SumAppliedAmt is "+ element.SumAppliedAmt);
Console.Write("SumCheckAmt is "+ element.SumCheckAmt);
}
或者,您可以分别使用ToList()
和ToArray
将其转换为常规列表/数组。
另外,例如,如果您确定该集合只有一个元素(因为您正在过滤checkId / transactionId组合),您可以使用First
或Single
,如下所示:< / p>
var sumOfData = fullData
.AsEnumerable()
.GroupBy(...)
.Where(...)
.Select(...)
.First();
将返回集合的第一个元素,不需要foreach
,所以你可以做
Console.Write("CheckID is "+ sumOfData.checkId);
Console.Write("transactionId is "+ sumOfData.transactionId);
Console.Write("SumAppliedAmt is "+ sumOfData.SumAppliedAmt);
Console.Write("SumCheckAmt is "+ sumOfData.SumCheckAmt);