通过LookUp / Something获取Child元素

时间:2016-03-06 07:00:06

标签: c# parent-child

我正在尝试获取父元素的子元素。我有一个基本数据库:

public class list
{
    public int id { get; set; }
    public List<Users> UsersList{ get; set; }

    public class Users
    {
        [Key]
        public int Users_id{ get; set; }
        public string UserId { get; set; }
    }
}

如果我想获取具有特定UserId的用户中的所有元素,我该怎么做?我试图避免使用嵌套的for循环并迭代数据库中List和Users的所有条目。我正在查找LookUp(),但在这种情况下如何应用它有点困惑。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

由于您有点困惑,我会提供比原始答案更多的详细信息。让我们来看看您的代码,并创建一个非常基本和粗略的示例程序:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace SOSample
{
    public class list
    {
        public int id { get; set; }
        public List<Users> UsersList { get; set; }

        public class Users
        {
            [Key]
            public int Users_id { get; set; }
            public string UserId { get; set; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Instantiate and initialize with sample data.
            var sampleList = new list()
            {
                id = 12345,
                UsersList = new List<list.Users>()
                {
                    new list.Users() { Users_id = 1, UserId = "0042" },
                    new list.Users() { Users_id = 2, UserId = "0019" },
                    new list.Users() { Users_id = 3, UserId = "0036" },
                    new list.Users() { Users_id = 4, UserId = "0214" },
                    new list.Users() { Users_id = 5, UserId = "0042" },
                    new list.Users() { Users_id = 6, UserId = "0042" },
                    new list.Users() { Users_id = 7, UserId = "0019" }
                }
            };

            // Linq search.
            var someId = "0042";
            var linqQuery = sampleList.UsersList.Where(user => user.UserId == someId);

            Console.WriteLine("Linq query results:");
            foreach (var r in linqQuery)
            {
                Console.WriteLine($"Users_id: {r.Users_id}, UserId: {r.UserId}");
            }

            // Lookup search (using same someId as for Linq).
            var lookup = sampleList.UsersList.ToLookup(user => user.UserId);
            var lookupQuery = lookup[someId];

            Console.WriteLine("\nLookup query results:");
            foreach (var r in lookupQuery)
            {
                Console.WriteLine($"Users_id: {r.Users_id}, UserId: {r.UserId}");
            }
        }
    }
}

输出:

Linq query results:
Users_id: 1, UserId: 0042
Users_id: 5, UserId: 0042
Users_id: 6, UserId: 0042

Lookup query results:
Users_id: 1, UserId: 0042
Users_id: 5, UserId: 0042
Users_id: 6, UserId: 0042

希望澄清事情。我在您的问题和评论中看到的主要问题是,您可能会将嵌套类误认为属性。实例化外部类时,内部类不会被实例化,也不是外部类的某种属性。

旧答案(提供个别详情):

我喜欢使用Linq。因此,假设sampleList的类型为list

var query = sampleList.UsersList.Where(user => user.UserId == someId);

那会给你IEnumerable<list.Users>。您始终可以使用ToList()ToArray()ToDictionary()来获取所需的集合类型:

var results = sampleList.UsersList.Where(user => user.UserId == someId).ToArray();

Lookup而言,我已经看到了一些使用它的方法,但对我来说最熟悉的方式是:

var lookup = sampleList.UsersList.ToLookup(user => user.UserId);
var query = lookup[someId];

再次,那将给你IEnumerable<list.Users>。或者,您可以从该查询中获取所选的集合类型:

var results = lookup[someId].ToArray();

基本上,您需要指定密钥在该查找中代表的内容(在这种情况下它是UserId),然后在时间到来时,您可以通过密钥进行搜索。