【问题标题】:List with in List using Lambda使用 Lambda 在列表中列出
【发布时间】:2019-02-11 09:56:04
【问题描述】:

我有两个表,我想合并生成报告

我用这段代码加入表格

db.tblProcessorLists.GroupJoin(
                 db.tbltransactions,
                 a => a.UserID,
                 b => b.UserID,
                 (x, y) => new
                 {
                     Processor = x,
                     Data = y.ToList()
                 }
         )
         .Select(a => new Report
         {
             Processor = a.Processor,
             TotalTrans = a.Data
         }).ToList();

这是输出

我想在我的报表类中插入数据。

这可能吗?如何实现?

编辑 我已经试过了,但它不起作用

db.tblProcessorLists.GroupJoin(
                 db.tbltransactions,
                 a => a.UserID,
                 b => b.UserID,
                 (x, y) => new
                 {
                     Processor = x,
                     Data = y.ToList()
                 }
         )
         .Select(a => new Report
         {
             Processor = a.Processor,
             SummaryTransaction = a.Data.Select(x=>new Summaryreport{ month=x.month, year=x.year, total=x.totaltransaction})
         }).ToList();

【问题讨论】:

  • @TDK 我已经添加了我尝试的内容
  • 使用 C# 和 LINQ 而不是 WPF 标记您的问题可能会得到更多答案(我在您的问题中没有看到任何 WPF)。

标签: c# wpf linq lambda


【解决方案1】:

虽然您没有指定,但我认为您想要的是汇总一个月的所有数据。所以而不是:

Processor Month   Value
    1    2018-1     2
    1    2018-1     3  => sum all 2018-1 values of processor 1: 2+3=5
    1    2018-2     4
    1    2018-2     5  => sum all 2018-2 values of processor 1: 4+5=9
    2    2018-1     1
    2    2018-1     2  => sum all 2018-1 values of processor 2: 1+2=3

您所要做的就是更改 GroupJoin 中的 ResultSelector,以便将所有数据分组到具有相同 [年、月] 的组中。结果是本年和本月所有值的[年、月、总和]]

查看Enumerable.GroupBy的重载之一

var result = db.tblProcessorLists.GroupJoin(  // GroupJoin processors and transactions
    db.tbltransactions,
    processor => processor.UserID,       // from every processor take the UserId
    transaction => transaction.UserID,   // from every transaction take the UserId

    // ResultSelector: use eacht processor with its zero or more matching transactions
    // to make one new object
    (processor, transactionsOfThisProcessor) => new
    {
        Processor = processor,

        // group all transactionsOfThisProcessor into groups with same [year, month]
        // and sum the values of all transactions in each group
        TransactionTotals = transactionsOfThisProcessor
            .GroupBy(transaction => new 
            {
                Year = transaction.Year,       // KeySelector: make groups with 
                Month = transaction.Month      // same [year, month]
            },

            // element selector: Transaction.Value
            transaction => transaction.Value,

            // ResultSelector: key = [year, month],
            // elements are all values of this [year, month] group
            // sum all values
            (key, valuesWithThisYearMonth) => new
            {
                Year = key.Year,
                Month = key.Month,
                Total = valuesWithThisYearMonth.Sum(),
            }),
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    相关资源
    最近更新 更多