问题
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();
回答1:
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
publichave them as properties. See this for some reference: Properties vs. Fields: Need help grasping the uses of Properties over Fields
来源:https://stackoverflow.com/questions/46007095/cannot-implicitly-convert-type-ienumerableob-to-generic-listcdsworkflows