C# - Linq有两个列表

时间:2018-04-25 11:01:11

标签: c# linq

我有2个课程通过ID连接在一起:

public class A {
    public int AID;
    public string Name;
}

public class B {
    public int BID;
    public int AID;
}

现在我要过滤我的listB以获取所有 B ,其中 A 名称是euqals参数 name

List<A> listA = ...;
List<B> listB = ...;

public List<B> Filter(string name) {
    var list = listB.Where(**... A.Name == name ...**).ToList();
    return list;
}

我不知道如何使用上面定义的参数过滤listB。也许有人可以帮我搞清楚。

谢谢!

5 个答案:

答案 0 :(得分:10)

使用 $scope.saveFormData = function (event,myForm) { //after your whole logic .... //to reset form myForm.$setPristine(); } Join。我更喜欢使用连接的查询语法:

Where

避免重复B的另一种方法(如果它是1对多的关系)正在使用查找:

IEnumerable<B> bs = 
    from a in listA
    join b in listB on a.AID equals b.AID
    where a.Name == name
    select b;
List<B> list = bs.ToList();

答案 1 :(得分:7)

虽然您可以使用B线性搜索过滤b.AID列表,但这样效率会很低:

return listB.Where(b => listA.FirstOrDefault(a => a.AID == b.AID)?.Name == name);

更好的方法是将listA中的条目放入Dictionary<int,A>,并使用b.AID的字典查找:

IDictionary<int,A> dictA = listA.ToDictionary(a => a.AID);
return listB.Where(b => dictA.TryGetValue(b.AID, out var a) && a.Name == name);

我强烈建议将A保留在dictA而不是listA,因为这样可以避免为每个查询重建字典。

答案 2 :(得分:4)

尝试如下所示加入linq

form b in listB 
join a in listA.Where(A.Name == name)
on a.AID equals b.AID
select b;

或首先获取id列表然后过滤掉

  var ids = from a in listA 
            where A.Name == name
            select a.AID;
  var Bs = from b in listB
            where ids.Contain(b.AID)
            select b;

答案 3 :(得分:2)

使用组连接以避免在listB中引入重复。

var filteredA = listA.Where(a => a.Name == name);
var query =
  from b in listB
  join a in filteredA on b.AID equals a.AID into matchedAgroup
  where matchedAgroup.Any()
  select b;

答案 4 :(得分:1)

我过去曾使用过这个简洁的子集列表:

    public void TestGetListBMatchingListA()
    {
        List<ClassA> listClassA = new List<ClassA>();
        listClassA.Add(new ClassA { ClassAId = 1, Name = "JayV1" });
        listClassA.Add(new ClassA { ClassAId = 2, Name = "JayV2" });
        listClassA.Add(new ClassA { ClassAId = 3, Name = "JayV3" });
        listClassA.Add(new ClassA { ClassAId = 4, Name = "JayV4" });
        listClassA.Add(new ClassA { ClassAId = 5, Name = "JayV5" });

        List<ClassB> listClassB = new List<ClassB>();
        listClassB.Add(new ClassB { ClassBId = 1, ClassAId = 1 });
        listClassB.Add(new ClassB { ClassBId = 2, ClassAId = 2 });
        listClassB.Add(new ClassB { ClassBId = 3, ClassAId = 3 });
        listClassB.Add(new ClassB { ClassBId = 4, ClassAId = 4 });
        listClassB.Add(new ClassB { ClassBId = 5, ClassAId = 1 });
        listClassB.Add(new ClassB { ClassBId = 6, ClassAId = 2 });
        listClassB.Add(new ClassB { ClassBId = 7, ClassAId = 3 });
        listClassB.Add(new ClassB { ClassBId = 8, ClassAId = 4 });
        listClassB.Add(new ClassB { ClassBId = 9, ClassAId = 5 });
        listClassB.Add(new ClassB { ClassBId = 10, ClassAId = 1 });

        var result = listClassB.Where(classB =>
                                        listClassA.Any(classA =>
                                        classB.ClassAId == classA.ClassAId 
                                        && classA.Name == "JayV1")).ToList();

        result.ForEach(r => Debug.WriteLine(r.ClassBId));
    }

它产生以下结果:

1
5
10