[英]Expression - Unable to create a constant value of type 'System.Collections.Generic.List`1'. Only primitive types ('such as Int32, String, and Gu
I have written following lambda expression 我写了以下lambda表达式
Expression<Func<ContractObject, bool>> objExpression =
i => i.ContractProjects.Any(a => ProjectList.Any(p => p.Id == a.ProjectId));
ContractProjects
and ProjectList
are List of two different types sharing common values ie. ContractProjects
和ProjectList
是共享公共值的两种不同类型的列表,即。 ProjectId and Id respectively. ProjectId和Id。
But it is throwing following exception. 但是它引发以下异常。 Is there any changes that I have to do or can this expression be written in some another way?
我是否需要做任何更改,或者可以用其他方式编写此表达式?
Unable to create a constant value of type 'System.Collections.Generic.List`1'.
无法创建类型为'System.Collections.Generic.List`1'的常量值。 Only primitive types ('such as Int32, String, and Guid') are supported in this context.
在这种情况下,仅支持基本类型(例如Int32,String和Guid)。
I think the problem is that your ContractList might not have been created yet 我认为问题在于您的ContractList可能尚未创建
try : 尝试:
var objExpression = i => i.ContractProjects.ToList()
.Any(a => ProjectList.Any(p => p.Id == a.ProjectId));
After many trial and error I got the following solution as follows 经过多次尝试和错误,我得到以下解决方案如下
List<Int32> projectIds = new List<Int32>();
foreach (Project p in ProjectList)
projectIds.Add(p.Id);
Expression<Func<ContractObject, bool>> objExpression =
i => i.ContractProjects.Any(a => projectIds.Contains(a.ProjectId)));
Thanks for helping 感谢您的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.