简体   繁体   English

从嵌套列表中获取项目

[英]Get items from nested list

I have a list (HashSet actually) of a class that contains 2 other lists. 我有一个包含2个其他列表的类的列表(实际上是HashSet)。 Here's a mockup with example data: 这是带有示例数据的模型:

public class Example
{
    public Example(HashSet<int> a, HashSet<int> b)
    {
        ListA = a;
        ListB = b;
    }

    public HashSet<int> ListA { get; set; }
    public HashSet<int> ListB { get; set; }
}

public class UseCase
{
    public UseCase()
    {
        var hs = new HashSet<Example>();
        hs.Add(new Example(new HashSet<int> { 1}, new HashSet<int> { 100, 200 }));
        hs.Add(new Example(new HashSet<int> { 2,3,4,5 }, new HashSet<int> { 100, 200, 300 }));
        hs.Add(new Example(new HashSet<int> { 6,9,12 }, new HashSet<int> { 200, 300 }));
    }
}

Here are the rules for the two lists: 这是两个列表的规则:

List A contains only unique numbers - they are never repeated, either within their own HashSet or any other ListA HashSet. 列表A仅包含唯一编号-在它们自己的HashSet或任何其他ListA HashSet中,它们永远不会重复。

List B contains numbers that are unique within their own HashSet, but they may be repeated in one or more List B HashSet. 列表B包含在其自己的HashSet中唯一的数字,但可以在一个或多个列表B HashSet中重复这些数字。

What I'm trying to do is return all numbers in matching ListA rows, where a specific number exists in ListB. 我想做的是返回匹配的ListA行中的所有数字,其中ListB中存在特定数字。

Here's a mockup linq query: 这是一个样机linq查询:

public static IEnumerable<int> GetDistinctFromListA(int b)
{
    return UsesCases
        .Where(x => x.ListB.Contains(b))
}

So, at this point I have a number of rows, but I now need to extract all numbers from matching ListB lists. 因此,这时我有很多行,但是现在我需要从匹配的ListB列表中提取所有数字。 If the parameter entered was 100, then I would expect to return a list containing the numbers 1, 2, 3, 4 and 5. 如果输入的参数是100,那么我希望返回一个包含数字1、2、3、4和5的列表。

I've been playing around with All and SelectMany but can't seem to get the result I need. 我一直在玩All和SelectMany,但似乎无法获得所需的结果。

public class Example
{
    public Example(HashSet<int> a, HashSet<int> b)
    {
        ListA = a;
        ListB = b;
    }

    public HashSet<int> ListA { get; set; }
    public HashSet<int> ListB { get; set; }
}

static IEnumerable<int> GetDistinctFromListA(HashSet<Example> hs, int b)
{
    var rv = hs.Aggregate(new HashSet<int>(), (acc, el) =>
    {
        if (el.ListB.Contains(b)) acc.UnionWith(el.ListA);
        return acc;
    });

    return rv;
}

static void Main(string[] args)
{
    var hs = new HashSet<Example>();
    hs.Add(new Example(new HashSet<int> { 1 }, new HashSet<int> { 100, 200 }));
    hs.Add(new Example(new HashSet<int> { 2, 3, 4, 5 }, new HashSet<int> { 100, 200, 300 }));
    hs.Add(new Example(new HashSet<int> { 6, 9, 12 }, new HashSet<int> { 200, 300 }));

    foreach (var b in hs.SelectMany(e => e.ListB).Distinct())
        Console.WriteLine($"{b} => {string.Join(",", GetDistinctFromListA(hs, b))}");

    Console.ReadLine();
}

This goes through all Elements of hs, checks each elements ListB if it contains b and if so adds its ListA elements to the result. 这遍历了hs的所有元素,检查每个元素ListB是否包含b,如果包含b,则将其ListA元素添加到结果中。

If you compare only Hashsets without depending on other Hashsets you can use Intersect / Union / Except etc. (set theory stuff). 如果仅比较哈希集而不依赖其他哈希集,则可以使用“相交” /“联合” /“除外”等(设置理论的东西)。

A HashSet is meant to contain only unique values and this is to say that all queries you make inherently return distinct values. HashSet只能包含唯一值,也就是说,您进行的所有查询本质上都返回不同的值。 If you want to find the intersection between two HashSet you can call the Intersect method. 如果要查找两个HashSet之间的交集,则可以调用Intersect方法。

For example: 例如:

//The additional 1 is ignored here
HashSet<int> A = new HashSet<int> { 1, 1, 2, 3, 4, 5 };
HashSet<int> B = new HashSet<int> { 1, 5, 8, 9 };
var Result = A.Intersect(B);

I think you want to return the intersection of 2 sets 我想你要归还2套的交集

There is a Linq function that does this 有一个Linq函数可以做到这一点

var commonInBoth = List1.Intersect(List2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM