LINQ Join with Multiple From Clauses

前端 未结 4 1807
耶瑟儿~
耶瑟儿~ 2020-12-08 02:18

When writing LINQ queries in C#, I know I can perform a join using the join keyword. But what does the following do?

from c in Companies
from e          


        
4条回答
  •  一个人的身影
    2020-12-08 02:23

    All the first set of objects will be joined with all the second set of objects. For example, the following test will pass...

        [TestMethod()]
        public void TestJoin()
        {
            var list1 = new List();
            var list2 = new List();
    
            list1.Add(new Object1 { Prop1 = 1, Prop2 = "2" });
            list1.Add(new Object1 { Prop1 = 4, Prop2 = "2av" });
            list1.Add(new Object1 { Prop1 = 5, Prop2 = "2gks" });
    
            list2.Add(new Object2 { Prop1 = 3, Prop2 = "wq" });
            list2.Add(new Object2 { Prop1 = 9, Prop2 = "sdf" });
    
            var list = (from l1 in list1
                        from l2 in list2
                        select l1).ToList();
    
            Assert.AreEqual(6, list.Count);
    
        }
    

提交回复
热议问题