亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 linq 左連接查詢中使用現有左模型預填充投影結果

如何在 linq 左連接查詢中使用現有左模型預填充投影結果

C#
慕雪6442864 2022-06-12 11:07:37
假設我有以下代碼(簡化)使用帶有左外連接的 linq to sql:public class Program{    public static void Main()    {               IList<Payment> paymentsList = new List<Payment>() {                 new Payment() { ID = 1, Amount = 4 } ,                new Payment() { ID = 2, Amount = -11 } ,                new Payment() { ID = 3, Amount = 11 }            };        IList<Allocation> allocationList = new List<Allocation>() {                 new Allocation(){ OriginalID = 1, ReversalID=2},                new Allocation(){ OriginalID = 2, ReversalID=3}                         };        var summaryPayments = from s in paymentsList                             join alloc in allocationList on s.ID equals alloc.OriginalID into allocOrg                            from po in allocOrg.DefaultIfEmpty()                            join allocRev in allocationList on s.ID equals allocRev.ReversalID into allocRevs                            from pr in allocRevs.DefaultIfEmpty()                            select new Payment{Amount=s.Amount, ReversalId = (pr != null ? pr.ReversalID : 0)};        foreach (var obj in summaryPayments)        {            Console.WriteLine("{0} - {1}", obj.Amount,obj.ReversalId);        }    }}public class Payment{    public int ID { get; set; }    public decimal Amount { get; set; }     public int? ReversalId { get; set; }    }public class Allocation{    public int OriginalID {get;set;}    public int ReversalID { get; set; } }
查看完整描述

1 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

試試下面的代碼


            var summaryPayments = (from s in paymentsList

                               join alloc in allocationList on s.ID equals alloc.OriginalID into allocOrg

                               from po in allocOrg.DefaultIfEmpty()

                               join allocRev in allocationList on s.ID equals allocRev.ReversalID into allocRevs

                               from pr in allocRevs.DefaultIfEmpty()

                               select new { s, pr }).Select(x => { x.s.ReversalId = (x.pr != null ? x.pr.ReversalID : 0); return x.s; });


查看完整回答
反對 回復 2022-06-12
  • 1 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號