[CSHARP-2321] exclude an member/field when use Expression to create Projection Definition Created: 06/Jul/18  Updated: 27/Oct/23  Resolved: 15/Jan/19

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

Type: Improvement Priority: Minor - P4
Reporter: Hongbo Liu Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

For example,

class Widget
{
    [BsonElement("x")]
    public int X { get; set; }
 
    [BsonElement("y")]
    public int Y { get; set; }
 
    [BsonElement("name")]
    public string Name { get; set; }
}

We can create the ProjectionDefinition like this:

var projection = Builders<Widget>.Projection<Widget>.Expression(x => new { X = x.X, Y = x.Y });

How about we want to use the expression to exclude a memeber/field?

Can we support something like this:

var projection = Builders<Widget>.Projection<Widget>.Expression(x => new { Name = null });

Basically in the expression, if the right-hand operand is default(T), we treat it as excluded field/member.

Is that possible?



 Comments   
Comment by Robert Stam [ 15/Jan/19 ]

It's an interesting thought, but I don't think it will work. Consider that the result of the projection (the TProjection type parameter) is by definition the same as the result type of the expression. In your second example TProjection will be an anonymous type like:

class <anonymous>
{
    public string Name { get; set; }
}

In  other words, the result type would contain only the Name, rather than all fields except the Name.

 

 

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