不能隐式转换类型'IEnumerable< ob>'到'Generic.List< cdsworkflows>'嵌套对象 [英] Cannot implicitly convert type 'IEnumerable<ob>' to 'Generic.List<cdsworkflows>' nested objects
问题描述
我有下面的类
public class cdsworkflows
{
public string WorkflowName;
public string ActionGroup;
}
公共类cdssystems
{
公共字符串cdsSystemName;
public列表< cdsmodules> listModules;
}
公共类cdsmodules
{
公共字符串moduleName;
public List< cdsworkflows> listWorkflows;
$ / code>
我将这些值从包含所有这些数据的表中推送到上述对象如下。在select new中出现错误:
无法隐式转换类型
System.Collections .Generic.IEnumerable< cdsworkflows>
到
System.Collections.Generic.List< cdsworkflows>
。有一个明确的
转换存在(你是否缺少演员?)
另外,如果您看到最后一行 ActionGroup = grpWorkflows.Count()。ToString()
我想分配值而不是计数。如何做到这一点。
List< cdssystems>结果=
(从myItemsList中的SPListItem项目
项目项目[系统]
到grp
选择新的cdssystems()
{
cdsSystemName = grp.Key.ToString(),
listModules =
from grp
group item by item [Modules]
into grpModules
select new cdsmodules( )
{
moduleName = grpModules.Key.ToString(),
listWorkflows =
from grpModules中的项目
项目项目[Workflows]
转换成grpWorkflow
选择新的cdsworkflows()
{
WorkflowName = grpWorkflows.Key.ToString(),
ActionGroup = grpWorkflows.Count()。ToString()
}
}
}
).ToList();
您的模型类包含 List< T>
,但在嵌套查询中,您不会向投影添加 ToList()
,因此它会创建 IEnumerable< T>
,因此错误:
对于 listWorkflows
:change to :
listWorkflows =(从grpModules中的项目
项目项目[工作流程]到grpWorkflows
选择新的cdsworkflows
{
WorkflowName = grpWorkflows.Key.ToString(),
ActionGroup = grpWorkflows.Count()。ToString()
})。ToList()
对cdsmodules有同样的想法
对于问题的第二部分,我希望将值和计数更改分配给:
ActionGroup = string.Join(,,grpWor kflows.Select(i =>我[[ActionGroup]]))
- 请参阅C#命名约定: MSDN , dotFactory
- 我建议如果您将字段定义为
public
,请将它们设为属性。请参阅以下参考资料:属性与字段:需要帮助了解属性在字段上的用法
I have the below classes
public class cdsworkflows
{
public string WorkflowName;
public string ActionGroup;
}
public class cdssystems
{
public string cdsSystemName;
public List<cdsmodules> listModules;
}
public class cdsmodules
{
public string moduleName;
public List<cdsworkflows> listWorkflows;
}
I am pushing the values to the above Objects from a table with all these data as below. I am getting error in "select new" that:
Cannot implicitly convert type
System.Collections.Generic.IEnumerable<cdsworkflows>
toSystem.Collections.Generic.List<cdsworkflows>
. An explicit conversion exists (are you missing a cast?)
Also if you see the last line ActionGroup = grpWorkflows.Count().ToString()
I want to assign the value and not the count. How to do that.
List<cdssystems> results =
(from SPListItem item in myItemsList
group item by item["Systems"]
into grp
select new cdssystems()
{
cdsSystemName = grp.Key.ToString(),
listModules =
from item in grp
group item by item["Modules"]
into grpModules
select new cdsmodules()
{
moduleName = grpModules.Key.ToString(),
listWorkflows =
from item in grpModules
group item by item["Workflows"]
into grpWorkflows
select new cdsworkflows()
{
WorkflowName = grpWorkflows.Key.ToString(),
ActionGroup = grpWorkflows.Count().ToString()
}
}
}
).ToList();
Your model classes contain properties of type List<T>
but in your nested queries you do not add ToList()
to the projection so it creates an IEnumerable<T>
and therefore the error:
For listWorkflows
: change to:
listWorkflows = (from item in grpModules
group item by item["Workflows"] into grpWorkflows
select new cdsworkflows
{
WorkflowName = grpWorkflows.Key.ToString(),
ActionGroup = grpWorkflows.Count().ToString()
}).ToList()
Same idea for cdsmodules
For the second part of the question of I want to assign the value and not the count change to:
ActionGroup = string.Join(", ", grpWorkflows.Select(i => i["ActionGroup"]))
Also:
- please refer to C# naming conventions: MSDN, dotFactory
- I suggest that if you define your fields as
public
have them as properties. See this for some reference: Properties vs. Fields: Need help grasping the uses of Properties over Fields
这篇关于不能隐式转换类型'IEnumerable< ob>'到'Generic.List< cdsworkflows>'嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!