为什么此LinQ查询不喜欢字符? [英] Why does this LinQ query not like chars?
问题描述
我有一个LINQ查询,它可以获取所需的所有数据并将其合并到数据传输对象中,一切正常,除了当我将DTO的一个成员(即char)设置为char值时会引发查询异常...
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...
类型为'System.ServiceModel.FaultException'的未处理异常 发生在mscorlib.dll
An unhandled exception of type 'System.ServiceModel.FaultException' occurred in mscorlib.dll
无法创建类型为'System.Char'的常量值.在这种情况下,仅支持基本类型或枚举类型.
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();
将性别设置为'm'只是行不通,从语法上讲还可以,只是在执行查询时不行!可能是什么问题?
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?
推荐答案
这是EF的局限性,因为char
不是原始类型,请参见此答案来映射字符的属性,以获取有关原因的更多详细信息.该限制本身也已记录.
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.
我建议您只使用string
.
这篇关于为什么此LinQ查询不喜欢字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!