簡體   English   中英

我如何使用 LINQ 加入、GroupBy 和 Select?

[英]How can i Join, GroupBy and Select with LINQ?

我正在嘗試將 Join、GroupBy 兩列和 select 執行到一個新列表中

            var v = master.Join(
                detail,
                o => o.Id,
                d => d.OrderId,
                (o, d) => new 
                {
                    DateAdded = o.DateAdded,
                    VatRate = d.VatRatePercentage,
                    InvoiceDescription = o.InvoiceDescription
                }).GroupBy(c => new
                {
                    c.DateAdded,
                    c.VatRate
                })
                .Select(gcs => new 
                {
                    DateAdded = gcs.Key.DateAdded.Date,
                    VatRate = gcs.Key.VatRate,
                    InvoiceTypeDescription = gcs.Key.InvoiceDescription,
                }
                ).ToList();

但我得到了錯誤

'<anonymous type: DateTime DateAdded, decimal VatRate>' does not contain a definition for 'InvoiceTypeDescription' and no accessible extension method 'InvoiceTypeDescription' accepting a first argument of type '<anonymous type: DateTime DateAdded, decimal VatRate>' could be found

我想按兩列(DateAdded,VatRate)分組,但在結果中也得到 InvoiceDescription 列。 我該如何解決?

這是一個示例:

var resultmutiplekeylambaorderbyelement = studentlist  
    .GroupBy(stu => new{stu.standard, stu.age})  
    .OrderBy(g => g.Key.standard).ThenBy(y=> y.Key.age)  
    .Select(g => new { standardkey = g.Key.standard, agekey = g.Key.age, studentobj = g.OrderBy(y => y.name) });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM