无法创建常量值-仅允许使用基本类型或枚举类型 [英] Unable to create a constant value - only primitive types or Enumeration types allowed
问题描述
我在这里看到了一些与此异常相关的问题,但没有一个使我理解问题的根本原因.所以这里还有一个...
I have seen some questions related to this Exception here but none made me understand the root cause of the problem. So here we have one more...
var testquery =
((from le in context.LoanEMIs.Include("LoanPmnt")
join lp in context.LoanPmnts on le.Id equals lp.LoanEMIId
where lp.PmntDtTm < date && lp.IsPaid == false
&& le.IsActive == true && lp.Amount > 0
select new ObjGetAllPendingPmntDetails
{
Id = lp.Id,
Table = "LoanEMI",
loanEMIId = lp.LoanEMIId,
Name = le.AcHead,
Ref = SqlFunctions.StringConvert((double)le.FreqId),
PmntDtTm = lp.PmntDtTm,
Amount = lp.Amount,
IsDiscard = lp.IsDiscarded,
DiscardRemarks = lp.DiscardRemarks
}).DefaultIfEmpty(ObjNull));
List<ObjGetAllPendingPmntDetails> test = testquery.ToList();
此查询给出以下异常消息-
This query gives the following Exception Message -
无法创建类型为
CashVitae.ObjGetAllPendingPmntDetails
的常数.在这种情况下,仅支持基本类型或枚举类型.
Unable to create a constant value of type
CashVitae.ObjGetAllPendingPmntDetails
. Only primitive types or enumeration types are supported in this context.
我添加了SQL函数语句以将le.FreqId
(它是byte
)转换为string
后,由于在LINQ Expression Store中无法识别ToString()
,所以得到了此异常.
I got this Exception after I added the SQL function statement to convert le.FreqId
which is a byte
to a string
as ToString()
is not recognized in the LINQ Expression Store.
ObjGetAllPendingPmntDetails
是我的模型中的局部类,由于在代码中使用了太多次而无法将数据绑定到表,因此添加了它.
它的两个ID
都一样长,'Amount'为十进制,PmntDtTm
为Datetime
,IsDiscard
为bool,其余所有都是包含'Ref'的字符串.
ObjGetAllPendingPmntDetails
is a partial class in my model which is added as it is used too many times in the code to bind data to tables.
It has both ID
s as long, 'Amount' as decimal, PmntDtTm
as Datetime
,IsDiscard
as bool and remaining all are string including 'Ref'.
我没有结果,因为目前没有数据满足条件.在尝试处理null时,我添加了DefaultIfEmpty(ObjNull)
,并且ObjNull
的所有属性都初始化如下.
I get no results as currently no data satisfies the condition. While trying to handle null, I added DefaultIfEmpty(ObjNull)
and ObjNull
has all properties initialized as follows.
ObjGetAllPendingPmntDetails ObjNull = new ObjGetAllPendingPmntDetails()
{ Id = 0, Table = "-", loanEMIId = 0, Name = "-", Ref = "-",
PmntDtTm = Convert.ToDateTime("01-01-1900"),
Amount = 0, IsDiscard = false, DiscardRemarks = "" };
我需要此查询才能正常工作,因为它已与其他5个查询一起调用了Union()
.全部返回相同的ObjGetAllPendingPmntDetails
列.但是存在一些问题,因为此查询没有满足条件和上面共享的异常的数据.
I need this query to work fine as it has Union()
called on it with 5 other queries. All returning the same ObjGetAllPendingPmntDetails
columns. But there is some problem as this query has no data satisfying the conditions and the Exception Shared Above.
任何建议都值得赞赏,因为我无法理解问题的根本原因.
Any suggestions are appreciated as I am unable to understand the root cause of the problem.
推荐答案
@AndrewCoonce是正确的,.DefaultIfEmpty(ObjNull)
是这里的罪魁祸首.实体框架将DefaultIfEmpty
变成类似...
@AndrewCoonce is right, the .DefaultIfEmpty(ObjNull)
is the culprit here. Entity Framework turns DefaultIfEmpty
into something like...
CASE WHEN ([Project1].[C1] IS NULL) THEN @param ELSE [Project1].[Value] END AS [C1]
...但是无法将ObjGetAllPendingPmntDetails
的实例强制转换为可以代替@param
的内容,因此会出现异常.
...but there's no way to coerce an instance of ObjGetAllPendingPmntDetails
into something that can take the place of @param
, so you get an exception.
如果将DefaultIfEmpty
调用移至ToList
之后,它应该可以正常工作(尽管如果您确实想要一个具体的列表实例,则需要在此之后再次调用ToList
).
If you move the DefaultIfEmpty
call to after the ToList
it should work correctly (although you'll need to call ToList
again after that if you really want a concrete list instance).
这篇关于无法创建常量值-仅允许使用基本类型或枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!