2

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?

Community
  • 1
  • 1
Sam
  • 586
  • 1
  • 6
  • 25
  • 9
    what exactly do the exceptions say? – Marc Gravell Sep 26 '13 at 10:26
  • Are you sure that `carer_gender` not throws custom exception in property implementation when one sets 'm' there? – Ryszard Dżegan Sep 26 '13 at 10:28
  • @MarcGravell - Sorry I forgot include that in the Q, the exception thrown is - _An unhandled exception of type 'System.ServiceModel.FaultException' occurred in mscorlib.dll_ – Sam Sep 26 '13 at 10:30
  • Please post ToString() of this exception so that we can see inner details. – Pavel Tupitsyn Sep 26 '13 at 10:32
  • @yBee - the DTO property isn't throwing the exception... – Sam Sep 26 '13 at 10:35
  • `FaultException` would indicate it's an issue going across the wire, not an issue with LINQ. It's worth posting your WCF method. – James Sep 26 '13 at 10:37
  • Further exception info (just had to switch the service into debug mode to get this) - Additional information: Unable to create a constant value of type 'System.Char'. Only primitive types or enumeration types are supported in this context. – Sam Sep 26 '13 at 10:41
  • @James - yeahh, learning on the fly ;) for the sake of what I am doing I can easily switch the DTO property to string, but I would like to know why this won't work... and not surrender to this issue! – Sam Sep 26 '13 at 10:47
  • @James - just saw your edit! Ohhh okay, pfttt what a strange issue! I did research prior to posting this question however it would have helped if I had got the _real_ exception information earlier (like you said). Thanks for the help, If you would like to post an answer stating the issue I would be happy to accept it :) Thanks again – Sam Sep 26 '13 at 10:52
  • @Sam I just made my comment an answer - "Convert to answer" feature on the comments? Might post that on meta :) – James Sep 26 '13 at 10:55
  • It's a database you are querying right? – Gayot Fow Sep 26 '13 at 12:34
  • @GarryVass - It certainly is, how come? – Sam Sep 26 '13 at 13:01
  • The query provider doesn't support it. See James' answer below... – Gayot Fow Sep 26 '13 at 13:18
  • @GarryVass - yeahh I had already accepted that answer, thanks though :) – Sam Sep 26 '13 at 13:34

3 Answers3

7

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.

Community
  • 1
  • 1
James
  • 80,725
  • 18
  • 167
  • 237
0

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;
Stephen Turner
  • 7,125
  • 4
  • 51
  • 68
0

If this is coming from VS and MVC using c# then Get rid of the apostrophe's and use full quotation marks instead.

use

carer_gender = "m"

instead of

carer_gender = 'm'

I've found that C# interprets the single quote as a char type instead of string.

In my case I had

var _list = (from a in table where string.Concat(a.AbsenceReasonCodeID, "|", a.Description).ToUpper().Contains(vlsTerm) select new { value = a.field1, label = String.Concat(a.field1, " : ", a.field2) }).ToList();

When I used single quotes I got the char error. Replacing them with double got rid of the problem.

Harry Binnendyk
  • 141
  • 1
  • 2