linq-leftjoin有多个表

时间:2018-04-20 07:33:02

标签: c# linq lambda

尝试使用2个表格获得左连接结果时,我遇到了一些问题 它显示了一些枚举错误。这里我有2个表-specs和users.It在迭代结果

时显示一些错误
class Program
        {
            class Specs
        {
            public int specId { get; set; }
            public string desc { get; set; }
            public int createdby { get; set; }
            public int lastupdatedby { get; set; }
        }

        class Users
        {
            public int userId { get; set; }
            public string username { get; set; }
        }

        class UpdatedUser
        {
            public int userId { get; set; }
            public string username { get; set; }
        }

        static void Main(string[] args)
        {
            var specs = new Specs[]
                {
                new Specs{specId = 1, desc = "Spec1", createdby=1, lastupdatedby=1},
                new Specs{specId = 2, desc = "Spec2", createdby=2, lastupdatedby=3},
                new Specs{specId = 3, desc = "Spec3", createdby=3, lastupdatedby=1},
                new Specs{specId = 4, desc = "Spec4", createdby=3, lastupdatedby=3},
            };

            var user = new Users[]
            {
                new Users{userId = 1, username = "User1"},
                new Users{userId = 2, username = "User2"},
            };

           var abc= specs.GroupJoin(user.DefaultIfEmpty(),
                s => s.lastupdatedby,
                u => u.userId,
                (s, u) => u.Select(x=>new{ specs = s, users = x })
                .Select(x => new {
                    specId = x.specs.specId,
                    desc = x.specs.desc,
                    createdby = x.specs.createdby,
                    username = x.users.username

                })).ToList();

        }
    }

0 个答案:

没有答案