简体   繁体   English

表达式-无法创建类型为'System.Collections.Generic.List`1'的常量值。 仅原始类型(例如Int32,String和Gu

[英]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. ContractProjectsProjectList是共享公共值的两种不同类型的列表,即。 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.

相关问题 显示无法创建类型“”的常量值。 在这种情况下,仅支持基本类型(例如Int32,String和Guid) - Showing Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context 无法创建类型为“结算类型”的常量值。 在这种情况下,仅支持基本类型(例如Int32,String和Guid) - Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context EF-Code First:无法创建类型&#39;&#39;的常量值。 在此上下文中仅支持原始类型(例如Int32,String和Guid&#39;) - EF-Code First: Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context 错误“无法将'System.Int32'类型的对象强制转换为'System.Collections.Generic.List`1 [System.Int32]'' - error “Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.List`1[System.Int32]'” IQueryable Union,无法创建类型为&#39;System.Collections.Generic.IEnumerable`1 [[System.Int32 - IQueryable Union, Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1[[System.Int32 ASP.NET LINQ错误无法创建类型为&#39;System.Collections.Generic.List`1 [System.Int32]&#39;的查询结果 - ASP.NET LINQ Error Cannot create a query result of type 'System.Collections.Generic.List`1[System.Int32]' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' 错误:无法创建类型为“ System.Object”的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context LINQ to实体错误:“无法创建类型为“ System.Int32 []”的空常量值。 仅实体类型,枚举类型 - LINQ to entity Error: “Unable to create a null constant value of type ''System.Int32[]”. Only entity types, enumeration types 此上下文仅支持原始类型(“例如 Int32、String 和 Guid”) - Only primitive types ('such as Int32, String, and Guid') are supported in this context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM