Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to specify .NET Anonymous object return type in Linq using Func<T,Q>?
    primarykey
    data
    text
    <p>How do you specify the <strong>Func</strong> signature for anonymous objects?</p> <pre><code>new Func&lt;DataSet, **IEnumerable&lt;int&gt;&gt;** </code></pre> <p>I am having a trouble with return type where I have specified as <strong>IEnumerable&lt;></strong> in the Func declaration</p> <p>Error I am getting from the expression is </p> <blockquote> <p>Cannot convert expression type 'System.Collections.Generic.IEnumerable&lt;{ParentNodeId:int}>' to returnt ype 'System.Collections.Generic.IEnumerable'</p> </blockquote> <p>How can I specify <strong>IEnumerable&lt;{ParentNodeId:int}></strong> in func?</p> <pre><code> public int GetCachedRootNodeId(IList&lt;int&gt; 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&lt;DataSet, IEnumerable&lt;int&gt;&gt;(resultSet =&gt; 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; } </code></pre> <p>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 <a href="https://stackoverflow.com/questions/576374/why-doesnt-c-have-lexically-nested-functions/576386#576386">Why doesn't C# have lexically nested functions?</a></p> <p><strong>EDIT</strong>: Final Result</p> <pre><code> public int GetCachedRootNodeId(IList&lt;int&gt; 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&lt;DataSet, IEnumerable&lt;int&gt;&gt;(resultSet =&gt; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload