57055

Why does this LinQ query not like chars?

I have a LINQ query that grabs all the data it needs and consolidates it into a data transfer object, everything works fine EXCEPT it throws query exceptions when I set one of the DTO's members (which is a char) to a char value...

An unhandled exception of type 'System.ServiceModel.FaultException' occurred in mscorlib.dll

Unable to create a constant value of type 'System.Char'. Only primitive types or enumeration types are supported in this context.

See the simplified query below:

var result = (from c in db.Foster_Carers where c.foster_carer_id == id join fc in db.Individual_Carers on c.first_carer_id equals fc.individual_carer_id select new FosterCarerPersonalInfoDTO { carer_title = fc.title, carer_forenames = fc.forename, carer_surname = fc.surname, carer_gender = 'm' }).SingleOrDefault();

Setting the gender to 'm' just won't work, syntactically it is okay, just not when the query is executed! What could be the issue?

Answer1:

This is a limitation of EF because char is not a primitive type, see this answer for more details as to why. The limitation itself is also documented.

I would suggest you just use string.

Answer2:

I've come across the same and had to store an intermediate value and do a conversion outside of the Linq query:

var intermediateResult = (from c in db.Foster_Carers where c.foster_carer_id == id join fc in db.Individual_Carers on c.first_carer_id equals fc.individual_carer_id select new { oldResult = new FosterCarerPersonalInfoDTO { carer_title = fc.title, carer_forenames = fc.forename, carer_surname = fc.surname, } , forConversion = new { carer_gender = "m" } }).SingleOrDefault(); intermediateResult.oldResult.carer_gender = intermediateResult.forConversion.carer_gender[0]; var result = intermediateResult.oldResult;

Recommend

  • Error Handling with WCF Service and Client Application
  • Extension method to Exception requiring dynamic generic type
  • CRM - sending email to contacts in sub grid
  • Cassandra eats memory
  • Get process output without blocking
  • Need to display iframe when link is clicked from menu
  • Getting p-values from leave-one-out in R
  • Vim syntax highlighting
  • Can't get WCF service's operations list with Web Service Studio client
  • In Ember.js, what's the difference between store save and store commit?
  • Windows 10 Bluetooth Gatt Client ValueChanged issue
  • Why can't pass only 1 coulmn to glmnet when it is possible in glm function in R?
  • add service reference xamarin PCL
  • Matlab Generating a Matrix
  • Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
  • How to write string.Contains(someText) in expression Tree
  • What do I do with this error when I run tests in rails?
  • Available space left on drive - WinAPI - Windows CE
  • Translating C# to PowerShell in InterIMAP
  • Authentication failed with Azure Active Directory in Windows Phone
  • NUnit 3.0 TestCase const custom object arguments
  • Unable to install Git-core+svn by MacPorts
  • Unable to decode certificate at client new X509Certificate2()
  • Time complexity of a program which involves multiple variables
  • Very simple C++ DLL that can be called from .net
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • ilmerge with a PFX file
  • Master page gives error
  • Launch Runnable Jar from Web Start
  • AES padding and writing the ciphertext to a disk file
  • Convert array of 8 bytes to signed long in C++
  • Google cloud sdk not working when python points python3
  • Comma separated Values
  • Linker errors when using intrinsic function via function pointer
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • LevelDB C iterator