I have a view in my database that produces ordered results, but when I run a Linq query over that view, the results are no longer ordered (at least, according to the foreach I use to iterate over the results, and according to the debugger). Is this a known difficulty with Linq, or am I missing something?
Update: my view, from SQL Server 2008 (from design view):
SELECT TOP (100) PERCENT UrlId, Title, Description, Url, Parent, ResourceKey, Published, dbo.udf_SiteMap_GetRoles(UrlId) AS Roles
FROM dbo.SiteMap
ORDER BY DisplayOrder
The Linq query that builds the SiteMap:
SqlConnection connection = new SqlConnection(_connect);
DataContext dc = new DataContext(connection);
Table<NodeRoleEntity> siteMapTable = dc.GetTable<NodeRoleEntity>();
var rootQuery = from ne in siteMapTable
where ne.ParentID == null
select ne;
foreach (NodeRoleEntity rootNode in rootQuery)
{
SiteMapNode root = rootNode.AsSiteMapNode(this);
base.AddNode(root, _root);
AddChildNodes(root, siteMapTable);
}
This query uses the SiteMap built above to render menus in my ASP.NET application:
StaticSiteMapProvider _provider = SiteMap.Providers["MySiteMap"] as StaticSiteMapProvider;
string cultureToken = _GetCulture().ToLower();
SiteMapNode cultureRoot =
(from SiteMapNode cr in _provider.RootNode.ChildNodes
where cr.Description == cultureToken
select cr).First();
int menuCount = 0;
foreach (SiteMapNode node in cultureRoot.ChildNodes)
{
_RenderMenu(node, menuCount.ToString(), writer);
menuCount++;
}
It is the nodes in cultureRoot.ChildNodes
that are ordered improperly (but the rows from which those nodes are derived are ordered).