none
Left Join的Lambda写法问题 RRS feed

  • 问题

  • 你好:

    我需要实现Select D.DcGuid, PD.DcGuid PDcGuid From Dept ,Dept PDept On Dept.ParentFId = PDept.DeptId.用Lambda写?

    Public Class Dept 
    {
        public string DeptFid {get;set;}
        public string ParentFid
        public string DcGuid {get;set;}
    }

    我尝试过:

    var deptOuGuidInfos = entities.DeptInfo
                                            //.Where(d => d.DeptFid == fid)
                                            .GroupJoin(
                                                entities.DeptInfo,
                                                dept => dept.ParentFid,
                                                pdept => pdept.DeptFid,
    
                                                (d, pd) => new
                                                {
                                                    D = d,
                                                    PD = pd.FirstOrDefault()
    
                                                })
                                            ;

    但是这样得到的是所有列,我需要筛选特定的列.


    ...

    2018年5月29日 7:45

全部回复

  • 你好,

    如果需要筛选第一列的话,可以参考下面的表达式,如果需要特定的列的,请加上where 条件。

    var query = (from p in db.DeptInfo
                                 join pd in db.DeptInfo on p.ParentFid equals pd.DeptFid
                                 select new
                                 {
                                     DcGuid = p.DcGuid,
                                     PDcGuid = pd.DcGuid
                                 }
                                ).FirstOrDefault();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年5月30日 2:05
    版主