[CSHARP-651] Support LINQ Cast operator. Created: 21/Dec/12  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Linq
Affects Version/s: 1.7
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: Evheny Nemov Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

In LINQ2SQL I could wrote:

public static int? GetIDByHash(this IQueryable<Picture> source, byte[] hash)
{
	return
		source
			.Where(p_ => p_.Hash == hash)
			.Select(p_ => (int?)p_.ID)
			.SingleOrDefault();
}

But in LINQ in MongoDB i can't do this. I have to do so:

public static int? GetIDByHash(this IQueryable<Picture> source, byte[] hash)
{
	var pic = source.SingleOrDefault(p => p.Hash == hash);
	return pic != null ? pic.ID : default(int?);
}

This saddens me.

The same issue appears with the Cast<int?>() operator.


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