[CSHARP-4498] .Project(c => c.Reference()) not work Created: 30/Jan/23  Updated: 27/Oct/23  Resolved: 30/Jan/23

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

Type: Bug Priority: Minor - P4
Reporter: 宇 杜 Assignee: Robert Stam
Resolution: Works as Designed Votes: 1
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-4763 Consider supporting client side proje... Scheduled
is duplicated by CSHARP-4588 LINQ3 LinqProvider do not support pub... Closed
is duplicated by CSHARP-4548 IFindFluent.Projection fails with Exp... Closed

 Description   

Summary

Driver in the new version (2.19.0) MongoDB. Exceptions are thrown when using the Project function. But in  version(2.18.0) is normal.

MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: c.Reference().

How to Reproduce

_eg:  https://github.com/joesdu/MongoIssue_



 Comments   
Comment by 宇 杜 [ 31/Jan/23 ]

thanks for your answer.

Comment by Robert Stam [ 30/Jan/23 ]

Thank you for reporting this issue.

The problem is this line:

https://github.com/joesdu/MongoIssue/blob/master/MongoIssue/Controllers/TestValuesController.cs#L38

Project(c => c.Reference())

 is not a valid MongoDB projection because there is no server-side equivalent to the user-defined C# Reference method.

This worked in LINQ2 because if something couldn't be translated to server-side MQL we would pull the data down to the client and execute part of the projection client-side.

LINQ3 specifically does NOT do that. Note that EF made the same choice some time ago to no longer do any hidden client side evaluation. It is considered an anti-pattern that hides potentially serious performance problems.

The solution is to make the client side processing explicit:

var data = await _db.TestValues.Find(_bf.Empty).ToListAsync();
var objs = data.Select(c => c.Reference()).ToList();

Comment by 宇 杜 [ 30/Jan/23 ]

https://github.com/joesdu/MongoIssue

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