-3

我有这个 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.checkIdsumOfData.transactionId?我想在不使用 foreach 循环的情况下访问它。对不起,我忘了添加那部分。我已经在使用一个 LINQforeach (var data in fullDataQuery)并在 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
4

3 回答 3

2

您可以像访问任何其他集合一样访问它。例如,您可以使用 a 迭代项目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();
于 2013-05-07T14:54:52.150 回答
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>. 但是因为你只能在这个方法中使用这个匿名类型,如果你想在不同的地方使用它,你应该使用一个真正的类。

于 2013-05-07T14:55:23.583 回答
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 组合),您可以使用Firstor Single,如下所示:

var sumOfData = fullData
                .AsEnumerable()
                .GroupBy(...)
                .Where(...)
                .Select(...)
                .First();

这将返回集合的第一个元素,消除了 a 的需要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);
于 2013-05-07T14:56:28.413 回答