LINQ的语法与排序依据自定义比较< T> [英] Linq syntax for OrderBy with custom Comparer<T>
问题描述
有两种格式与自定义排序比较器任何给定的LINQ表达式:
格式1
VAR的查询=
源
。选择(X =>新建{x.someProperty,x.otherProperty})
.OrderBy(X =>的x,新myComparer());
格式2
VAR的查询=
从X源
排序依据点¯x//比较器表达式放在这里?
选择新的{x.someProperty,x.otherProperty};
问:结果
是什么语法?该订单在第二格式表达
不是问题:结果
如何使用自定义的比较器作为在第一格式显示
奖金积分:结果
是否有实际的,这两个格式的Linq正式名称上市以上?
什么是语法的顺序逐表达在第二格式<? / p>
它不存在。从排序依据条款文档:
<块引用>
您也可以指定一个自定义比较。然而,这仅仅是通过使用基于方法的句法提供。
块引用>
如何使用自定义比较第一格式。
块引用>
您正确地写的。您可以通过
的IComparer< T>
为你写
<块引用>
在那里为上述两个LINQ的格式实际,正式的名字呢?
块引用>
格式1被称为基于方法的语法(从以前的链接)和格式2查询表达式语法 (从这里)。
There are two formats for any given Linq expression with a custom sort comparer:
Format 1
var query = source .Select(x => new { x.someProperty, x.otherProperty } ) .OrderBy(x => x, new myComparer());
Format 2
var query = from x in source orderby x // comparer expression goes here? select new { x.someProperty, x.otherProperty };
Question:
What is the syntax for the order-by expression in the second format?Not the question:
How to use a custom comparer as shown in the first format.Bonus credit:
Are there actual, formal names for the two Linq formats listed above?解决方案What is the syntax for the order-by expression in the second format?
It doesn't exist. From the orderby clause documentation:
You can also specify a custom comparer. However, it is only available by using method-based syntax.
How to use a custom comparer in the first format.
You wrote it correctly. You can pass the
IComparer<T>
as you wrote.
Are there actual, formal names for the two Linq formats listed above?
Format 1 is called "Method-Based Syntax" (from previous link), and Format 2 is "Query Expression Syntax" (from here).
这篇关于LINQ的语法与排序依据自定义比较< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!