我试图提出一组链接OrderBy
/ ThenBy
扩展方法调用,它们等同于使用orderby
关键字的LINQ语句.
我使用orderby
关键字的LINQ语句如下所示:
List<Summary> sortedSummaries = new List<Summary>(
from summary in unsortedSummaries
orderby summary.FieldA ascending,
summary.FieldB ascending,
summary.FieldC ascending,
summary.FieldD ascending
select summary);
Run Code Online (Sandbox Code Playgroud)
现在,我猜想使用链式OrderBy
/ ThenBy
扩展方法调用的等效版本看起来像这样:
List<Summary> sortedSummaries = new List<Summary>(unsortedSummaries);
sortedSummaries.OrderBy(x => x.FieldA).ThenBy(x => x.FieldB).ThenBy(x => x.FieldC).ThenBy(x => x.FieldD);
Run Code Online (Sandbox Code Playgroud)
但是,与使用orderby
关键字的LINQ语句相比,这给出了完全不同的结果.
我在这里做错了什么?
我试图转换为使用链式OrderBy
/ ThenBy
扩展方法调用的原因是我需要使用自定义比较器FieldD
,如下所示:
.ThenBy(x => x.FieldD, new NaturalSortComparer())
Run Code Online (Sandbox Code Playgroud)
我无法orderby
想象如何使用关键字的LINQ语句来做到这一点,所以我认为使用扩展方法可能会让我更远,但因为我无法获得我的扩展方法版本给我与我的orderby
关键字版本相同的结果,我现在回到绘图板.
有任何想法吗?
导入Linq命名空间时,可以同时使用.Count()和.Count(例如,在实现IList的对象上).
.Count()将调用.Count属性(如果可用),如下所示: C#Count()扩展方法性能
是否更好地直接使用.Count而不是.Count(),以获得(小)性能增益?
更新:这里有趣的性能数据(1-2个数量级):Linq Count()是否比List.Count或Array.Length更快或更慢?
检查IEnumerable集合是否具有多于或少于X个元素以实现谓词的最佳方法是什么?
我目前正在使用,.Count(lambda) <= limit
但这使程序不必要地遍历整个集合.
我已经看到了这一点,但我想知道是否有可能强制覆盖未标记为虚拟或抽象类型的成员(因此不应该破坏基础对象的功能).
我特别指的是覆盖:
IDictionary<TKey, TValue> Dictionary { get; }
Run Code Online (Sandbox Code Playgroud)
在
KeyedCollection<TKey, TValue>
Run Code Online (Sandbox Code Playgroud)
使用另一个实现IDictionary的成员类型,以使用另一个内部存储集合.
是否有可能绕过限制?
请注意,我不想为此使用扩展(在我的情况下,它无论如何都不会有用).
我正在使用哈希集结构进行映射:Dictionary<string, string>
.
如果我需要在连续语句中多次读取myHashset ["key1"]的值,那么使用局部变量存储第一个查找是一个好习惯吗?
编辑:没有其他线程会修改字典,因此它不是一个重要的标准.
谢谢!
在 Python 3.5 中,我尝试for
使用dict
理解来迭代循环,但它似乎不像list
. 以下内容将被报告为语法错误(不是由 PyCharm 报告,仅在运行时报告):
for k, v in (k, v for k, v in {"invalid": 1, "valid": 2}.items() if k == "valid"): # syntax error
print("Valid: " + (str(v)))
Run Code Online (Sandbox Code Playgroud)
虽然以下工作有效:
for e in (e for e in ["valid", "invalid"] if e == "valid"): # works
print(e)
Run Code Online (Sandbox Code Playgroud)
我知道创建一个新dict
的会起作用(例如),但我想避免开销,因为我只是对元素进行操作。
for k, v in {k: v for k, v in my_dict.items() if k == "valid"}.items(): # works
print("Valid: " + (str(k)))
Run Code Online (Sandbox Code Playgroud)
当然,我可以 …
我正在尝试创建一个SelectMany()
无需任何查询选择器即可工作的扩展(因此根本没有参数)。我想出了以下几点:
public static IEnumerable<V> SelectMany<T, V>(this IEnumerable<T> enumerable) where T : IEnumerable<V> {
return enumerable.SelectMany(e => e);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试像这样调用它时,类型参数无法被识别:
var d = new Dictionary<string, List<decimal>>();
var values = d.Values.SelectMany();
Run Code Online (Sandbox Code Playgroud)
无法从用法中推断出方法“CollectionsExtensions.SelectMany(IEmumerable”) 的类型参数。请尝试显式指定类型参数。
它仅适用于:
public static IEnumerable<V> SelectMany<K, V>(this Dictionary<K, List<V>>.ValueCollection enumerable) {
return enumerable.SelectMany(e => e);
}
Run Code Online (Sandbox Code Playgroud)
我缺少什么?
c# ×7
.net ×5
linq ×2
.net-3.5 ×1
comparison ×1
count ×1
decimal ×1
dictionary ×1
generics ×1
inheritance ×1
loops ×1
mysql ×1
oop ×1
overriding ×1
performance ×1
precision ×1
python ×1
sorting ×1
syntax-error ×1