Skip to main content
 首页 » 编程设计

C# 将两个集合连接在一起

2024年02月04日30mate10pro

C# 中的全新尝试将两个集合连接在一起,最终结果将提供来自两个集合的所有数据。现在代码只返回匹配的文档。谢谢。

  //Collections 
   IQueryable collection1 = database.GetCollection("MainData").AsQueryable(); 
   IQueryable collection2 = database.GetCollection("SecondaryData").AsQueryable();  
 
          var result = collection2.Join(collection1, a => a.ID, b => b.ID, (a, b) => new { a, b }) 
                .ToList() 
                .Select(s => new ViewModel.Customer() 
                { 
                    ID = s.b.ID, 
                    FirstName = s.b.FirstName 
                    LastName = s.b.LastName 
                }).ToList(); 
 
 
            return result;

请您参考如下方法:

You could use Concat which will return the items from the first collection followed by the items from the second collection:

var combined = collection1.Concat(collection2);

Or you could use Union which will return distinct values from both collections:

var combined = collection1.Union(collection2);