1. 程式人生 > >Linq兩表聯查結果集處理

Linq兩表聯查結果集處理

全域性定義一個同結果集型別的變數:

var strQuery =(new int[] { 1 }).Select(x => new { DispLevel = "empty",
                    Separator ="empty",
                    GrpShortCut = "empty" ,
                    AddToolBar ="empty",
                    FuncName ="empty",
                    FuncDesc ="empty",
                    FuncTypeFlag ="empty",
                    FuncGroup ="empty",
                    AssemblyFile="empty",
                    AssemblyName="empty",
                    ShortCut="empty",
                    IconIndex=Convert.ToDecimal(0)
                }).ToList();

區域性查詢的結果集:

strQuery = ctx.Secgrpfuncrel.Where(t => t.Factory == sFactory && t.ProgramId == sProgramId && t.SecGrpId == sSecGrpId && t.DispLevel.CompareTo(sDispLevel) >= 0).Join(
                        ctx.Secfunction,
                        a => new { f = a.FuncName },
                        b => new { f = b.FuncName },
                        (a, b) => new
                        {
                            a.DispLevel,
                            a.Separator,
                            GrpShortCut = a.ShortCut,
                            a.AddToolBar,
                            b.FuncName,
                            b.FuncDesc,
                            b.FuncTypeFlag,
                            b.FuncGroup,
                            b.AssemblyFile,
                            b.AssemblyName,
                            b.ShortCut,
                            b.IconIndex,
                        }
                        ).OrderBy(t => t.FuncName).ToList();

注:

1、兩表相同欄位,其中一個取別名(GrpShortCut = a.ShortCut,別名=[欄位名])

2、注意看報錯資訊,變數欄位型別不同會導致