New Posts New Posts RSS Feed: LINQ nullable types
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

LINQ nullable types

 Post Reply Post Reply
Author
francois View Drop Down
Newbie
Newbie
Avatar

Joined: 12-Feb-2009
Posts: 12
Post Options Post Options   Quote francois Quote  Post ReplyReply Direct Link To This Post Topic: LINQ nullable types
    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
Back to Top
francois View Drop Down
Newbie
Newbie
Avatar

Joined: 12-Feb-2009
Posts: 12
Post Options Post Options   Quote francois Quote  Post ReplyReply Direct Link To This Post 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()));
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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) };
Back to Top
ouelletf View Drop Down
Newbie
Newbie
Avatar

Joined: 15-May-2009
Location: Québec, Canada
Posts: 9
Post Options Post Options   Quote ouelletf Quote  Post ReplyReply Direct Link To This Post 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 ?

Back to Top
francois View Drop Down
Newbie
Newbie
Avatar

Joined: 12-Feb-2009
Posts: 12
Post Options Post Options   Quote francois Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down