加入linq查询中的对象列表

时间:2018-03-26 09:42:21

标签: c# linq join linq-to-objects multiple-tables

我尝试加入"用户"与#34;接收者列表"并创建ResultClass。用户可能有也可能没有Receiver对象。 Receiver对象包含UserIds列表,我希望这些Id:s替换为正确的用户。

以下是我尝试做的和我想要的示例程序。

class Program
{
    class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public override string ToString(){return Name;}
    }

    class UserIdHolder
    {
        public int Id { get; set; }
        public int UserId { get; set; }
    }

    class ReceiverTable
    {
        public int Id { get; set; }
        public int UserId { get; set; } //User, whos entry this is.
        public string OtherProperty { get; set; }
        public ICollection<UserIdHolder> LinkedUserIds { get; set; } //Linked users
    }

    class ResultClass
    {
        public User User { get; set; }
        public string OtherProperty { get; set; } //additional data
        public ICollection<User> LinkedUsers { get; set; } //null / empty if none exists
        public override string ToString() { return User.ToString(); }
    }

    public static void Main(string[] args)
    {
        User user1 = new User { Id = 1, Name = "User1" }; //Has table, with 2 linked users
        User user2 = new User { Id = 2, Name = "User2" };
        User user3 = new User { Id = 3, Name = "User3" }; //Has table, with 3 linked users
        User user4 = new User { Id = 4, Name = "User4" }; //Has table, without linked users
        User user5 = new User { Id = 5, Name = "User5" };

        ReceiverTable User1Table = new ReceiverTable
        {
            Id = 1,
            UserId = 1,
            OtherProperty = "Something for User1Table",
            LinkedUserIds = new List<UserIdHolder>
            {
                new UserIdHolder(){ Id  = 1, UserId = 2},
                new UserIdHolder(){ Id  = 2, UserId = 3}
            }
        };

        ReceiverTable User3Table = new ReceiverTable
        {
            Id = 2,
            UserId = 3,
            OtherProperty = "Something for User3Table",
            LinkedUserIds = new List<UserIdHolder>
            {
                new UserIdHolder(){ Id  = 3, UserId = 2},
                new UserIdHolder(){ Id  = 4, UserId = 4},
                new UserIdHolder(){ Id  = 5, UserId = 5}
            }
        };

        ReceiverTable User4Table = new ReceiverTable
        {
            Id = 3,
            UserId = 4,
            OtherProperty = "Something for User4Table",
            LinkedUserIds = new List<UserIdHolder>()
        };


        //"Database"
        List<User> _users = new List<User> { user1, user2, user3, user4, user5 };
        List<ReceiverTable> _receivers = new List<ReceiverTable> { User1Table, User3Table, User4Table };

        var query = from user in _users
                    join rt in _receivers on user.Id equals rt.UserId into rvTemp
                    from subRecv in rvTemp
                    from linkedUser in subRecv.LinkedUserIds
                    join u in _users on linkedUser.UserId equals u.Id into linkedUserTmp
                    select new ResultClass
                    {
                        User = user,
                        OtherProperty = subRecv.OtherProperty,
                        LinkedUsers = linkedUserTmp.ToList()
                    };

        //query should contain 5 items, all with unique User
        //if user has ReceiverTable entry, it should be added with Other property and linked users
        var uniqueUsers = query.GroupBy(q => q.User);
        if (uniqueUsers.Count() != 5)
            Console.WriteLine("Failed");
        else
            Console.WriteLine("Success!");

        Console.ReadKey();
    }

所以现在我得到5个User对象不唯一的对象。我需要5个对象,每个对象都有唯一的用户(用户1 - 5)和其他属性设置,如果有任何设置。所以我不理解这个linq逻辑,我还没有找到解决这个问题的好方法。

为了澄清,我希望_users表中的每个User都有ResultClass对象。

0 个答案:

没有答案