Tag: ienumerable

确定LINQ Enumerable是否包含基于条件的对象?

我有一个IEnumerable 我想知道这个列表是否有任何元素Project.ID == someID 。 有没有办法做到这一点?

IEnumerable为DataTable性能问题

我有以下扩展,它从IEnumerable生成一个DataTable : public static DataTable AsDataTable(this IEnumerable enumerable) { DataTable table = new DataTable(); T first = enumerable.FirstOrDefault(); if (first == null) return table; PropertyInfo[] properties = first.GetType().GetProperties(); foreach (PropertyInfo pi in properties) table.Columns.Add(pi.Name, pi.PropertyType); foreach (T t in enumerable) { DataRow row = table.NewRow(); foreach (PropertyInfo pi in properties) row[pi.Name] = t.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, […]

通用列表为IEnumerable

我正在尝试将List转换为IEnumerable,因此我可以validation不同的列表不为null或为空: 假设myList是List 。 然后在我想要的来电代码中: Validator.VerifyNotNullOrEmpty(myList as IEnumerable, @”myList”, @”ClassName.MethodName”); 有价值的代码是: public static void VerifyNotNullOrEmpty(IEnumerable theIEnumerable, string theIEnumerableName, string theVerifyingPosition) { string errMsg = theVerifyingPosition + ” ” + theIEnumerableName; if (theIEnumerable == null) { errMsg += @” is null”; Debug.Assert(false); throw new ApplicationException(errMsg); } else if (theIEnumerable.Count() == 0) { errMsg += @” is empty”; […]

试图实现一个可以比较任何两个列表的方法,但它总是返回false

我正在尝试创建一个方法,可以比较任何两个列表的相等性。 我试图以一种validation一个列表的每个元素与另一个列表的每个元素具有相同值的方式来比较它们。 下面的My Equals方法总是返回false ,有人能看出为什么会这样吗? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class IEnumerableComparer : IEqualityComparer<IEnumerable> { public bool Equals(IEnumerable x, IEnumerable y) { for(int i = 0; i<x.Count();i++){ if(!Object.Equals(x.ElementAt(i), y.ElementAt(i))){ return false; } } return true; } public int GetHashCode(IEnumerable obj) { if (obj == null) return 0; return unchecked(obj […]

c#接口实现 – 为什么这不构建?

很抱歉,如果之前有人询问过,但谷歌几乎不可能。 我认为int数组实现IEnumerable,因此Thing应该能够实现IThing。 怎么没有? public interface IThing { IEnumerable Collection { get; } } public class Thing : IThing { public int[] Collection { get; set; } } 注意 public class Thing : IThing { public int[] Array { get; set; } public IEnumerable Collection { get { return this.Array; } } } 很好。

IEnumerable 的扩展方法?

我有一堆不同的枚举,比如…… public enum MyEnum { [Description(“Army of One”)] one, [Description(“Dynamic Duo”)] two, [Description(“Three Amigo’s”)] three, [Description(“Fantastic Four”)] four, [Description(“The Jackson Five”)] five } 我为任何Enum编写了一个扩展方法,以获取Description属性(如果有)。 很简单吧…… public static string GetDescription(this Enum currentEnum) { var fi = currentEnum.GetType().GetField(currentEnum.ToString()); var da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute)); return da != null ? da.Description : currentEnum.ToString(); } 我可以非常简单地使用它,它就像一个魅力,返回描述或ToString()按预期。 这是问题所在。 我希望能够在IEnumerable的MyEnum,YourEnum或SomeoneElsesEnum上调用它。 所以我简单地编写了以下扩展名。 public […]

如何使用Directory.EnumerateFiles,不包括隐藏文件和系统文件

我正在枚举目录中的所有文件,以便稍后处理它们。 我想排除隐藏和系统文件。 这是我到目前为止: IEnumerable<IGrouping> files; files = Directory.EnumerateFiles(sourcePath, “*”, SearchOption.AllDirectories) .Where(f => (new FileInfo(f).Attributes & FileAttributes.Hidden & FileAttributes.System) == 0) .GroupBy(Path.GetDirectoryName); 但是,如果我查看结果,我仍然会隐藏并包含系统文件: foreach (var folder in files) { foreach (var file in folder) { // value for file here still shows hidden/system file paths } } 我发现这个问题与杰罗姆有类似的例子,但我甚至无法编译。 我究竟做错了什么?

在c#中,如何在multithreading环境中迭代IEnumerable

我在这种情况下,有一个大字典,由一个线程以相当高的频率随机更新,还有另一个线程试图将字典的快照保存为历史记录。 我目前正在使用这样的东西: Dictionary dict = new Dictionary(); var items = dict.Values.ToList(); 这在大多数情况下都可以正常工作,除非它偶尔抛出: System.InvalidOperationException:集合已被修改; 枚举操作可能无法执行。 我理解为什么会这样,但我不知道我该怎么做才能避免收集修改错误。 迭代此类集合的最佳方法是什么? 我也试过ConcurrentDictionary,但没有运气。 为什么? ConcurrentDictionary线程只在项目级别安全吗?

Enumerable.ElementAt 的重点是什么?

IEnumerable公开了一个枚举器,因此可以枚举该对象。 此接口公开的索引没有任何内容。 IList与索引有关,因为它公开了IndexOf方法。 那么Enumerable.ElementAt的重点是什么? 我刚刚阅读了这个LINQ扩展方法的文档 : 返回序列中指定索引处的元素。 嗯,是的,这是一个序列 ,而不仅仅是一个IEnumerable 。 阅读评论: 如果源类型实现IList,则该实现用于获取指定索引处的元素。 否则,此方法获取指定的元素。 好的,所以如果具体类型实现了从IList (这是一个实际序列)inheritance的东西,那么它与IndexOf()相同。 如果不是,则迭代直到达到索引。 这是一个示例场景: // Some extension method exposed by a lib // I know it’s not a good piece of code, but let’s say it’s coded this way: public static class EnumerableExtensions { // Returns true if all elements are ordered […]

在foreach循环内手动增加枚举数

我在foreach循环中有一个嵌套的while循环,我希望在满足某个条件时无限期地推进枚举器。 为此,我尝试将枚举器转换为IEnumerator (如果它在foreach循环中必须是这样)然后在已转换对象上调用MoveNext(),但它给出了一个错误,说我无法转换它。 无法通过引用转换,装箱转换,取消装箱转换,包装转换或空类型转换将类型“System.DateTime”转换为System.Collections.Generic.IEnumerator。 foreach (DateTime time in times) { while (condition) { // perform action // move to next item (time as IEnumerator).MoveNext(); // will not let me do this } // code to execute after while condition is met } 在foreach循环中手动增加IEnumerator的最佳方法是什么? 编辑:编辑显示有一个条件满足后我想要执行的w​​hile循环之后的代码这就是为什么我想在内部手动递增然后突破它而不是继续这会让我回到顶部。 如果这是不可能的,我相信最好的事情就是重新设计我是如何做到的。