简体   繁体   English

Linq2Sql连接到DefaultIfEmpty

[英]Linq2Sql Join Into DefaultIfEmpty

I have 2 tables that I need to grab columns from based on cid which is ClientID in both tables. 我有2个表格,我需要根据两个表格中的ClientID的cid来获取列。 My code below works in displaying the table but _DOCID is always null. 我下面的代码可用于显示表,但_DOCID始终为null。 What am I missing? 我想念什么?

            public IQueryable ReturnCart(int cid)
    {
        try 
        {
            var result = from c in veps.ecomCarts
                         join d in veps.lawFirmClientDocumentPurchaseds on c.ClientID equals d.ID into cd
                         from d in cd.DefaultIfEmpty()
                         where c.ClientID == cid
                            select new
                         {
                             _DOCID = d.ID,
                             _ID = c.ID,
                             _CID = c.ClientID,
                             _UPC = c.UPC,
                             _Description = c.Description,
                             _Quantity = c.Quantity,
                             _UnitPrice = c.UnitPrice,
                             _Discount = c.Discount,
                             _Total = c.Total,
                             _InProgress = c.InProgress,
                             _dts = c.dts

                         };


            return result;
        }
        catch (Exception ex)
        {
            _IsError = true;
            _ErrorMsg = ex.Message;
            return null;
        }
    }

Updated Issue Image: enter image description here 更新的问题图片: 在此处输入图片说明

So after much thinking in my sleep I realized that the answer to my issue was GroupBy Nested. 因此,经过多想,我意识到我的问题的答案是GroupBy Nested。 Here is my final code. 这是我的最终代码。

public IQueryable ReturnCart(int cid)
    {
        try 
        {
            var result = (from c in veps.ecomCarts
                          where c.ClientID == cid
                          select new 
                          {

                              _ID = c.ID,
                              _CID = c.ClientID,
                              _UPC = c.UPC,
                              _Description = c.Description,
                              _Quantity = c.Quantity,
                              _UnitPrice = c.UnitPrice,
                              _Discount = c.Discount,
                              _Total = c.Total,
                              _InProgress = c.InProgress,
                              _dts = c.dts,

                              lcdp = from d in veps.lawFirmClientDocumentPurchaseds
                                     select new
                                     {
                                         _DOCID = d.ID
                                     }
                          });

            return result;
        }
        catch (Exception ex)
        {
            _IsError = true;
            _ErrorMsg = ex.Message;
            return null;
        }
    }

Thank you Ivan for posting. 谢谢Ivan发布。 You actually helped me think of what needed to be done. 您实际上帮助了我思考需要做什么。 So thanks for that. 非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM