我有DataTable一个Name专栏.我想生成按字母顺序排列的唯一名称的集合.以下查询忽略order by子句.
var names =
(from DataRow dr in dataTable.Rows
orderby (string)dr["Name"]
select (string)dr["Name"]).Distinct();
Run Code Online (Sandbox Code Playgroud)
为什么不orderby加强执行?
我有LINQ查询,它是以零碎的方式构建的,如下所示:
var initialQuery = from item in MyContext where xxx == yyy select item;
var furtherQuery = from item in initialQuery where bla == foo select new { some stuff };
// more code here...
// eventually:
var yetAnotherQuery = (from item in furtherQuery ...)
.OrderBy(my_condition);
// As far as I know, the following query should still maintain the order of the previous one
// see: https://stackoverflow.com/questions/911942/are-subqueries-in-linqtosql-guaranteed-to-be-in-the-same-order-as-their-parent
var stillAnotherQuery = (from item in yetAnotherQuery
select item.data_I_care_about)
.Distinct();
// And finally... …Run Code Online (Sandbox Code Playgroud)