ienumerable

LINQ学习之旅——准备(1)

我们两清 提交于 2020-01-19 05:52:49
  其实LINQ在语法上很多都是运用了C#语言的一些特性,C#语言从2.0升级到3.0,并没有升级C#2.0的运行平台CLR,只是在原有语法基础上添加了新的特性,这些特性依赖于C#3.0的编译器。使用C#3.0的编译器编译过的代码可以在支持C#2.0的运行平台CLR上运行,即完全可以在.NET2.0平台上运行编译过的.NET3.0代码。C#2.0中的泛型,匿名方法,以及C#3.0中的Lambda表达式、表达式树以及扩展方法都是LINQ的基础。这一节我将介绍有关C#2.0中的泛型、委托、匿名方法、yield关键字以及IEnumerable接口的知识,这些和LINQ的实现和使用有着密切的关系。是我们学习之旅的第一项准备工作。   1.目前大部分语言都是强类型语言,这些语言要求程序中的变量或对象要有明确的类型,在不同类型之间的转化需要遵守一定的规则。强类型的编程有助于代码的安全性,但因为限制多了,就会显得不够灵活。如下代码所示: 1 int Max(int x,int y)2 {3 if(a<b)4 return b;5 else6 return a;7 } 比较两个整数的大小。但是这两个参数都是强类型int,如果我要比较两个双精度、浮点数类型或两者都有的数据,那就需要在定义几个新的方法了,可是这几个方法最终的方法体确实一样的,这样就使得代码有了冗余性。也许有读者会问

Core源码(四)IEnumerable

时光总嘲笑我的痴心妄想 提交于 2020-01-18 12:04:44
首先我们去core的源码中去找IEnumerable发现并没有,如下 Core中应该是直接使用.net中对IEnumerable的定义 自己实现迭代器   迭代器是通过IEnumerable和IEnumerator接口来实现的,今天我们也来尝试实现自己的迭代器。   首先来看看这两个接口: internal interface IEnumerable { [DispId(-4)] System.Collections.IEnumerator GetEnumerator(); } public interface IEnumerator { object Current { get; } bool MoveNext(); void Reset(); }   并没有想象的那么复杂。其中IEnumerable只有一个返回IEnumerator的GetEnumerator方法。而IEnumerator中有两个方法加一个属性。   接下来,我们继承IEnumerable接口并实现: public class MyIEnumerable : IEnumerable { private string[] strList; public MyIEnumerable(string[] strList) { this.strList=strList; } public IEnumerator

RadioButton doesn't get current value from model

橙三吉。 提交于 2020-01-17 08:37:31
问题 My model is IEnumerable<> of RatingSource. public class RatingSource { public int Id; public string Source; public bool IsActive; In the view I want to see 2 columns: Source and radioButton, cheked if IsActive is true. I write: @foreach (var ratingSource in Model) { <tr> <td> @Html.DisplayFor(modelItem => ratingSource.Source) @Html.RadioButton("Active",ratingSource.IsActive) <br /> </td> </tr> } But radioButtons are always unchecked. Where is a mistake? 回答1: have you tried: @Html

ASP.NET MVC2 displaying 2 lists of data in a strongly typed view

六眼飞鱼酱① 提交于 2020-01-17 01:42:07
问题 I am having difficulties displaying a list of data that incoporates two database objects. My view is strongy typed to a view model that includes a list of customers and a list of customer sites (each stored in their own table). Im currently using two for each statments to render both lists, but really i need one list that contains both objects, in a nutshell its a list of customer sites that also contains customer names from the customer table. This is my viewmodel namespace CustomerDatabase

Avoid Collection has been modified error

别来无恙 提交于 2020-01-15 07:15:23
问题 Issue: I have the following code: foreach(var ItemA in GenericListInstanceB) { ItemA.MethodThatCouldRemoveAnyItemInGenericListInstanceB(); } Obviously i get an error. What i want to do is change GenericListInstanceB to a class i create, that keeps track of items it should remove. Then, when the loop is finished, it removes them all. Same with adding items. This is fine, i can create a class that has an internal list, and a list of items to add and items to remove. Then another method called

Unable to step into or break in method called inside Linq query/expression

故事扮演 提交于 2020-01-13 19:20:49
问题 I have encountered a bizarre issue trying to step into a method called from within a Linq query (although I also experience the problem when using a Linq expression). The code compiles, AND it appears to work (I get the outcome I was expecting). IEnumerable<TagBuilder> theOutcome = from r in resultSet select GetTableDataRow(null); OR IEnumerable<TagBuilder> theOutcome = resultSet.Select(r => GetTableDataRow(null)); The method being called is defined as follows: private static TagBuilder

Illegal characters in path. Error when using Skip and Take with IEnumerable

我的未来我决定 提交于 2020-01-13 09:09:14
问题 Hi there I have the following action in my controller [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index() { IEnumerable<SurveyResult> myresult = filterData(); totalCount = myresult.Count<SurveyResult>(); ViewBag.totalCount = totalCount; //myresult = myresult.Skip<SurveyResult>(100 * pageIndex).Take<SurveyResult>(100); return View(myresult); } Here is my view @model IEnumerable<SurveyResult> @{ ViewBag.Title = "Survey Results"; } @using (Html.BeginForm()) { <table class="std-tbl"> <thead

Find the highest number in a set to be rounded down, and round it up instead

自古美人都是妖i 提交于 2020-01-13 02:17:34
问题 As the title describes, I have a set of objects - call them Allocations - which contain a description & a number. All numbers in the set add up to 100%, but for display purpose I sometimes round to a whole percent. In some edge cases, having rounded the numbers I end up with 99%. Example: Description | Actual | Rounded =============================== Allocation A | 65.23% | 65% Allocation B | 25.40% | 25% Allocation C | 7.95% | 8% Allocation D | 1.42% | 1% ===============================

In which cases do I need to create two different extension methods for IEnumerable and IQueryable?

馋奶兔 提交于 2020-01-12 13:44:41
问题 Let's say I need an extension method which selects only required properties from different sources. The source could be the database or in-memory collection. So I have defined such extension method: public IQueryable<TResult> SelectDynamic<TResult>( this IQueryable<T> source, ...) This works fine for IQueryable s. But, I have to call this function also for IEnumerable s. And in that case, I can call it with the help of .AsQueryable() : myEnumerable.AsQueryable() .SelectDynamic(...) .ToList();

In which cases do I need to create two different extension methods for IEnumerable and IQueryable?

↘锁芯ラ 提交于 2020-01-12 13:44:31
问题 Let's say I need an extension method which selects only required properties from different sources. The source could be the database or in-memory collection. So I have defined such extension method: public IQueryable<TResult> SelectDynamic<TResult>( this IQueryable<T> source, ...) This works fine for IQueryable s. But, I have to call this function also for IEnumerable s. And in that case, I can call it with the help of .AsQueryable() : myEnumerable.AsQueryable() .SelectDynamic(...) .ToList();