Print Page | Close Window

LINQ nullable types

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1257
Printed Date: 22-Sep-2025 at 9:34am


Topic: LINQ nullable types
Posted By: francois
Subject: LINQ nullable types
Date Posted: 14-May-2009 at 8:05am
Hi,

I get a weird exception: "Unknown Expression type: IIF(e.Accredited.Value, 1, 0)"
running the following statement:

var x = from e in _EntityManager.TrainingCourses
            select new { Disabled = (e.Accredited.Value ? 1 : 0) };
 
e.Accredited is defined as bool?

Please help!! How do I evaluate a (bool?) in the select.

Thanks



Replies:
Posted By: francois
Date Posted: 14-May-2009 at 8:32am
Better example of the whole bool? problem. Disabled select throws an exception
public class ReportDatasourceHelper {
	private BeesActsEntityManager EntityManager {
		get;
		set;
	}
	public IEnumerable GetEmployeeDetails() {
		var x = ( from emp in EntityManager.PersonSets.OfType<Employee>()
			 select new {
					EmpID = emp.IDNumber,
					Gender = emp.Gender.Name,
					Name = emp.FirstName,
					Surname = emp.Surname,
					Disabled = ((emp.Disabled.HasValue && emp.Disabled.Value) ? 1 : 0)
					//Disabled = Convert.ToInt32(( emp.Disabled.HasValue && emp.Disabled.Value ))
				    } ).OrderBy(r => r.EmpID);
		return x;
	}
}
and this get called to bind to a datasource for a RS report
ReportDatasourceHelper helper = new ReportDatasourceHelper;
helper.EntityManager = _EntityManager;
ReportViewer.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("Employee_Details", helper.GetEmployeeDetails()));


Posted By: kimj
Date Posted: 14-May-2009 at 12:11pm
Unfortunately, the DevForce EntityQuery doesn't support the conditional expression, e.Accredited.Value ? 1 : 0, which is causing the weird exception.  I've opened a bug for this, and we'll get it fixed in the next release.
 
As a workaround for now you'll need to first bring the results into cache, and then project from those results:
 
var x = from e in _EntityManager.TrainingCourses.ToList()
            select new { Disabled = (e.Accredited.HasValue && e.Accredited.Value ? 1 : 0) };


Posted By: ouelletf
Date Posted: 15-May-2009 at 10:03am
If it is only conditionnal expression that are not supported.
I think only use (xxx.HasValue && xxx.Value) should work.

ex.:
var x = from e in _EntityManager.TrainingCourses
            select new { Disabled = (e.Accredited.HasValue && e.Accredited.Value) };

Am i wrong ?
Does EF translate the HasValue && Value to SQL correctly ?



Posted By: francois
Date Posted: 17-May-2009 at 5:53am
Hi,
 
Good suggestion but there are two problems with this solution.
 
1: Disabled = (e.Accredited.HasValue && e.Accredited.Value) does not return 1 or 0. This only works with boolean. What if the types were (int?). Never implement a stop gap solution that is not type interchangable. It makes code harder to maintain when types change in the future. e.g. to an enumeration when true false is no longer the only values.
 
2: It seems .HasValue and .Value is also not supported when mapping to SQL.
 
The .ToList() is the only solution.
 
Thanks fo the workaround.
 
Francois



Print Page | Close Window