[CSHARP-4623] Linq Union Not Working Created: 20/Apr/23  Updated: 25/Apr/23  Resolved: 25/Apr/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Unknown
Reporter: Uday Dangrecha Assignee: Patrick Gilfether
Resolution: Duplicate Votes: 0
Labels: Union, c#, unionWith
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-4627 Support Union via $unionWith in LINQ3 Closed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

When trying to union two different collections the driver gives "Expression not supported" error.

MongoDB Version: 5.0.3

.NET Driver Version: 2.18.0
Linq Provider: V3

How to Reproduce

1) Create two IQueryable variables for two different collections selecting some fields

2) Combine both of variables created above with Union operation and assign it to new/third variable

3) Try to get total count of records from union.

      • Code ***

 

var fpQuery = (from fanPass in _AGLMongoDBContext.FanPasses.AsQueryable()
select new
{
FanPassId = fanPass.FanPassId,
CreatedDate = fanPass.CreatedDate
}).AsQueryable();
 
var fpHistoryQuery = (from fanPassHistory in _AGLMongoDBContext.FanPassesSubscriptionHistory.AsQueryable()
select new
{
FanPassId = fanPassHistory.FanPassId,
CreatedDate = fanPassHistory.CreatedDate
}).AsQueryable();
 
var fpUnion = fpQuery.Union(fpHistoryQuery).AsQueryable();
 
var fpCount = fpUnion.Count();

 

 

      • Expression of fpQuery variable ***

 

agl.FanPasses.Aggregate([{ "$project" : { "FanPassId" : "$FanPassId", "CreatedDate" : "$CreatedDate", "_id" : 0 } }])

 

 

      • Expression of fpHistoryQuery variable ***

 

agl.FanPassesSubscriptionHistory.Aggregate([{ "$project" : { "FanPassId" : "$FanPassId", "CreatedDate" : "$CreatedDate", "_id" : 0 } }])

 

 

      • Expression of fpUnion variable ***

 

agl.FanPasses.Aggregate([]).Select(fanPass => new <>f__AnonymousType1035`2(FanPassId = fanPass.FanPassId, CreatedDate = fanPass.CreatedDate)).Union(agl.FanPassesSubscriptionHistory.Aggregate([]).Select(fanPassHistory => new <>f__AnonymousType1035`2(FanPassId = fanPassHistory.FanPassId, CreatedDate = fanPassHistory.CreatedDate))) 

 

 

      • Error ***

 

Expression not supported: agl.FanPasses.Aggregate([]).Select(fanPass => new <>f__AnonymousType1035`2(FanPassId = fanPass.FanPassId, CreatedDate = fanPass.CreatedDate)).Union(agl.FanPassesSubscriptionHistory.Aggregate([]).Select(fanPassHistory => new <>f__AnonymousType1035`2(FanPassId = fanPassHistory.FanPassId, CreatedDate = fanPassHistory.CreatedDate))). 

 

      • Field Data Type ***

        FanPassId -> long / Int64
        CreatedDate -> DateTime 

         



 Comments   
Comment by James Kovacs [ 25/Apr/23 ]

Our LINQ provider does not currently support IQueryable<T>.Union. You can use Fluent Aggregate's UnionWith to perform this operation in the meantime.

var coll1 = db.GetCollection<BsonDocument>("coll1");
var coll2 = db.GetCollection<BsonDocument>("coll2");
 
var query = coll1.Aggregate().UnionWith(coll2).Count();

I have filed CSHARP-4627 to implement IQueryable<T>.Union in LINQ3. Please follow that ticket for updates.

Generated at Wed Feb 07 21:48:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.