-
Type:
New Feature
-
Resolution: Gone away
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: LINQ
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Mongodb collection join using C# driver and LINQ. I found two relevant solutions list below
var table1 = _myRepo.GetAll(); var table2 = table1.Where(x => x.Type == "MyType"); var try1 = ( from x in table1 join y in table2 on new { a = x.ClaimId, b = x.CensusId } equals new { a = y.ClaimId, b = y.CensusId } select x ) .ToList();
This query will throw exception The Join query operator is not supported.
var try2 =
(
from x in table1
from y in table2
where x.ClaimId == y.ClaimId && x.CensusId == y.CensusId
select x
)
.Select(x => x)
.ToList();
This query will throw exception The SelectMany query operator is not supported
But below query works
var table1 = _arRepository.GetAll(); var table2 = table1.Where(x => x.Type == "MyType"); var claimIds = table2.Select(x => x.ClaimId).Distinct().ToList(); var censusIds = table2.Select(x => x.CensusId).Distinct().ToList(); var data = table1.Where(x => claimIds.Contains(x.ClaimId) && censusIds.Contains(x.CensusId));
The problem with this is that it loads the (claimIds and censusIds ) in memory, Actually I want to executes this on server so that it return only required data. I also found a blog for newly mongodb feature (Lookup) discussed in
Blog: https://www.axonize.com/blog/iot-technology/joining-collections-in-mongodb-using-the-c-driver-and-linq/ I flow the blog to install required drivers
<PackageReference Include="mongocsharpdriver" Version="2.9.2" /> <PackageReference Include="MongoDB.Bson" Version="2.9.2" /> <PackageReference Include="MongoDB.Driver" Version="2.9.2" /> <PackageReference Include="MongoDB.Driver.Core" Version="2.9.2" />