How do you specify the Func signature for anonymous objects?
new Func<DataSet, **IEnumerable<int>>**
I am having a trouble with return type where I have specified as IEnumerable<> in the Func declaration
Error I am getting from the expression is
Cannot convert expression type 'System.Collections.Generic.IEnumerable<{ParentNodeId:int}>' to returnt ype 'System.Collections.Generic.IEnumerable'
How can I specify IEnumerable<{ParentNodeId:int}> in func?
public int GetCachedRootNodeId(IList<int> fromNodeIds, int forNodeId)
{
var result = forNodeId;
const string spName = "spFetchAllParentNodeIDs";
using (var ds = _df.ExecuteDatasetParamArray(_ConnectionString, spName, forNodeId))
{
if (DataAccessUtil.DataSetIsEmpty(ds)) return result;
var orderByLevelDesc = new Func<DataSet, IEnumerable<int>>(resultSet =>
from DataRow row in DataAccessUtil.GetFirstTableRows(resultSet)
orderby DataAccessUtil.GetInt32(row, "Level") descending
select new { ParentNodeId = DataAccessUtil.GetInt32(row, "ParentNodeID") });
//// Get top-most parent's node ID first (higher the level, the more top-most the parent is)
//var query = from DataRow row in DataAccessUtil.GetFirstTableRows(ds)
// orderby DataAccessUtil.GetInt32(row, "Level") descending
// select new { ParentNodeId = DataAccessUtil.GetInt32(row, "ParentNodeID") };
//foreach (var nodeInfo in query)
foreach (var nodeInfo in orderByLevelDesc(ds))
{
if (fromNodeIds.Contains(nodeInfo.ParentNodeId))
return nodeInfo.ParentNodeId;
}
}
return result;
}
By the way, I could have used commented code "query" and be done with it. But wanted to be more expressive and try something new after looking at this answer Why doesn't C# have lexically nested functions?
EDIT: Final Result
public int GetCachedRootNodeId(IList<int> fromNodeIds, int forNodeId)
{
var result = forNodeId;
const string spName = "spFetchAllParentNodeIDs";
using (var ds = _df.ExecuteDatasetParamArray(_ConnectionString, spName, forNodeId))
{
if (DataAccessUtil.DataSetIsEmpty(ds)) return result;
var orderParentNodeIDByLevelDesc = new Func<DataSet, IEnumerable<int>>(resultSet =>
from DataRow row in DataAccessUtil.GetFirstTableRows(resultSet)
orderby DataAccessUtil.GetInt32(row, "Level") descending
select DataAccessUtil.GetInt32(row, "ParentNodeID"));
foreach (var parentNodeId in orderParentNodeIDByLevelDesc(ds))
{
if (fromNodeIds.Contains(parentNodeId))
return parentNodeId;
}
}
return result;
}