Tag: ienumerable

安全地检查不可重复的IEnumebles是否空虚

有时候检查一个不可重复的 IEnumerable以查看它是否为空是有帮助的。 LINQ的Any对此不起作用,因为它消耗了序列的第一个元素,例如 if(input.Any()) { foreach(int i in input) { // Will miss the first element for non-repeatable sequences! } } (注意:我知道在这种情况下没有必要进行检查 – 这只是一个例子!真实世界的例子是对一个可能为空的右手IEnumerable执行一个Zip 。如果它是空的,我希望结果是原样的左手IEnumerable 。) 我想出了一个看起来像这样的潜在解决方案: private static IEnumerable NullifyIfEmptyHelper(IEnumerator e) { using(e) { do { yield return e.Current; } while (e.MoveNext()); } } public static IEnumerable NullifyIfEmpty(this IEnumerable source) { IEnumerator e = […]

使用DataContext从LINQ查询填充DataTable的最快方法

我正在尝试运行linq查询,但我需要将结果作为数据表,因为我使用它来存储来自同一viewstate对象中的不同查询的记录。 下面的两个版本编译,但返回一个空集。 确切的错误是“值不能为空。参数名称:源” 。 (是的,我检查过有数据): MyDatabaseDataContext db = new MyDatabaseDataContext(conn); IEnumerable queryProjects = (from DataRow p in db.STREAM_PROJECTs.AsEnumerable() where p.Field(“STREAM_ID”) == StreamID select new { PROJECT_ID = p.Field(“PROJECT_ID”), PROJECT_NAME = p.Field(“PROJECT_NAME”) }) as IEnumerable; DataTable results = queryProjects.CopyToDataTable(); … //(from p in db.STREAM_PROJECTs.AsEnumerable() //where p.STREAM_ID == StreamID //select new //{ // p.PROJECT_NAME, // p.PROJECT_ID //}) […]

包装IEnumerable并捕获exception

我有一堆可以Process()对象的类,并返回自己的对象: public override IEnumerable Process(IEnumerable incoming) { … } 我想编写一个可以包装其中一个处理器的处理器类,并记录包装的Process()方法可能抛出的任何未捕获的exception。 我的第一个想法是这样的: public override IEnumerable Process(IEnumerable incoming) { try { foreach (var x in this.processor.Process(incoming)) { yield return x; } } catch (Exception e) { WriteToLog(e); throw; } } 但这不起作用,因为CS1626:不能在带有catch子句的try块的主体中​​产生值 。 所以我想写一些概念上等同但编译的东西。 :-)我有这个: public override IEnumerable Process(IEnumerable incoming) { IEnumerator walker; try { walker = this.processor.Process(incoming).GetEnumerator(); […]

将IEnumerable列表传递给控制器

在我的asp.net MVC 4项目中,我正在尝试将IEnumerable列表从视图传递到控制器。 问题是在动作中接收的列表为空。 请帮忙。 这是我的代码的一部分 观点: @model IEnumerable @using (Ajax.BeginForm(“AddNewLeader”, “Equipe”, FormMethod.Post, new AjaxOptions { UpdateTargetId = “Leader”, HttpMethod = “Post”, InsertionMode = InsertionMode.Replace })) { foreach (var item in Model) { @Html.HiddenFor(modelItem => item.ID_agent) @Html.RadioButtonFor(modelItem => item.SelectedAgent, item.ID_agent) @Html.DisplayFor(modelItem => item.nom_agent) @Html.DisplayFor(modelItem => item.prenom_agent) } } 控制器: public ActionResult AddNewLeader(IEnumerable ListAgent) { […] […]

如何将两个IEnumerables合并(或压缩)?

我有一个IEnumerable和一个IEnumerable我想要合并到IEnumerable<KeyValuePair> ,其中KeyValuePair中连接在一起的元素的索引是相同的。 注意我没有使用IList,所以我没有计算我正在合并的项目或索引。 我怎样才能做到最好? 我更喜欢LINQ的答案,但任何能够以优雅的方式完成工作的东西都会起作用。

从IEnumerable 集合中删除项目

我有一个popuplated IEnumerable集合。 我想从中删除一个项目,我该怎么做? foreach(var u in users) { if(u.userId = 1123) { // remove! } } 我知道你不应该在循环时删除,所以我不介意创建新集合或删除它。 但我不知道如何删除一个项目,由于某种原因丢失了! 或者我也很困惑,我怎么能创建一个新的集合,如: IEnumerable modifiedUsers = new List(); foreach(var u in users) { if(u.userId != 1233) { modifiedUsers.add ?????? } } 如何添加到集合中?

连接多个IEnumerable

我正在尝试实现一个连接多个List的方法,例如 List l1 = new List { “1”, “2” }; List l2 = new List { “1”, “2” }; List l3 = new List { “1”, “2” }; var result = Concatenate(l1, l2, l3); 但我的方法不起作用: public static IEnumerable Concatenate(params IEnumerable List) { var temp = List.First(); for (int i = 1; i < List.Count(); i++) […]

两个枚举之间的平等

我有两个具有完全相同的参考元素的枚举,并想知道为什么Equals不是真的。 作为一个侧面问题,下面的代码比较每个元素的作用,但必须有一个更优雅的方式 var other = (ActivityService) obj; if (!AllAccounts.Count().Equals(other.AllAccounts.Count())) return false; for (int i = 0; i < AllAccounts.Count(); i++) { if (!AllAccounts.ElementAt(i).Equals(other.AllAccounts.ElementAt(i))) { return false; } } return true;

c#4.0:int一个真实的对象子类型? 协方差,可数和值类型

我想知道为什么IEnumerable不能分配给IEnumerable 。 毕竟IEnumerable是支持协方差的少数接口之一…… 子类型关系和协方差的东西适用于引用类型 int似乎是object的正确子类型 两种function的组合不起作用…… class A { } class B : A { } class Program { static void Main(string[] args) { bool b; b = typeof(IEnumerable).IsAssignableFrom(typeof(List)); Console.WriteLine(“ienumerable of ref types is covariant: ” + b); //true b = typeof(IEnumerable).IsAssignableFrom(typeof(List)); Console.WriteLine(“ienumerable of value tpyes is covariant: ” + b); //false b = typeof(object).IsAssignableFrom(typeof(int)); […]

在IEnumerable 中选择偶数/奇数元素?

可能重复: 使用LINQ获取序列的奇数/偶数部分 如何从List 中获取每个第n项? 我正在使用HtmlAgilityPack和C#来解析一些HTML。 基本上,我有这些元素,每个元素都在自己的对象中,在IEnumerable中。 是否有一种优雅的方式来获取集合中的每个N / 2元素。 意思是,用类.ruler跳过每个div? 我需要遍历生成的集合,因此要么将每个找到的对象复制到新的IEnumerable中,要么在foreach函数中使用它内联。 例如: //Copying resulting set to new IEnumerable: var odds = elements.SelectOdds(); //Using it inline for my usage: foreach (var x in elements.SelectOdds()) { } 哪种选择最好,我怎样才能做到这一点?