ienumerable

What is the real advantage of returning ICollection<T> instead of a List<T>? [duplicate]

北城以北 提交于 2019-12-12 08:20:04
问题 This question already has answers here : Closed 10 years ago . I've read a couple of blog post mentioning that for public APIs we should always return ICollection (or IEnumerable) instead of List. What is the real advantage of returning ICollection instead of a List? Thanks! Duplicate: What is the difference between List (of T) and Collection(of T)? 回答1: An enumerator only returns one entity at a time as you iterate over it. This is because it uses a yield return . A collection, on the other

IEnumerable to list inside linq query

馋奶兔 提交于 2019-12-12 03:36:52
问题 I am trying to run this query, where I try to fetch categories and there sub categories studentCont.ContinuumCategories = db.continuumcategorymasters .Where(x => x.AssessmentId == assessmentId && x.ContinuumCategory != "other") .Select(x => new ContinuumCategory() { AssessmentId = x.AssessmentId, ContinuumCategoryId = x.ContinuumCategoryId, NativeAppid = x.NativeAppCategoryId, score = 0, ContinuumCategoryName = x.ContinuumCategory, ContinuumSubCategories = x.continuumsubcategorymasters

How to exchange the items of enumeration by interating only once?

[亡魂溺海] 提交于 2019-12-12 02:56:04
问题 I'm trying to exchange the ordering of particular items of an IEnumerable . Given an IEnumerable<int> a; of the elements: 1, 2, 3, 4, 5 and what I want to do is write a exchange iterator which resulting a.Exchange(1, 2) in: 1, 3, 2, 4, 5 But I don't want the enumerable be iterated more than once with this simple purpose. What I have so far is: public static IEnumerable<T> Exchange<T>( this IEnumerable<T> source, int index1, int index2) { var i=0; foreach(var y in source) { if(index1==i) { var

Casting an IEnumerable to IEnumerable<T> with reflection

本小妞迷上赌 提交于 2019-12-12 02:50:50
问题 So, I need to call a third party method, that has a signature like this ThirdPartyMethod<T>(IEnumerable<T> items) My problem is I don't know the type of my object at compile time. At compile time I have this IEnumerable myObject; Type typeOfEnumerableIHave; Sooo..can reflection help me out here somehow? for simplicity sake, pretend I have a method like this void DoWork(IEnumerable items, Type type) { //In here I have to call ThirdPartyMethod<T>(IEnumerable<T> items); } 回答1: Since you have

disadvantages of using IQueryable !

我的未来我决定 提交于 2019-12-12 02:28:50
问题 Is there are any performance issues related to using IQueryable ? Besides, if I use a cursor instead of using IQueryable (is that better) . IQueryable vs IEnumerable vs IList ? I use MongoDB as my database . Thank you 回答1: I don't know how the MongoDB C# binding works, but describe how it usually works: When using IQueryable an expression tree is constructed, then translated into a format the database can understand and then executed in the database-server. This typically has a small overhead

ASP.NET passing List back to controller from IEnumerable @model inside Table header ActionLink with non-indexing

自古美人都是妖i 提交于 2019-12-12 02:28:25
问题 I have been reviewing possible ways to return a View's @model information which is of type IEnumerable back to the controller, so that if I sort/filter on a query from database, I can refine the return each iteration without restarting with a fresh full list being returned. All the ways show you need to POST back based on model[index] which works if you are inside a for loop. But I am working with sending the collection back from an @HTML.ActionLink within a table's header section, so there

Registration form with roles MVC3, Error: ViewData item is of type “system.sting” must be "IEnumerable<selectlistitem>

一个人想着一个人 提交于 2019-12-12 02:27:48
问题 I'm using MVC3 with razor and I wanna make a registration form where I can choose role with a dropdownlist. I get the roles in the dropdown list and all that but when i click submit i get: The ViewData item that has the key 'Role' is of type System.String but must be of type IEnumerable<SelectListItem> . My code Model: [Required] [Display(Name = "Select role: ")] public String Role { get; set; } My controller: public ActionResult Register() { List<SelectListItem> list = new List

Getting the index of a sequence of items

冷暖自知 提交于 2019-12-12 01:37:58
问题 I was trying to get the index of a sequence of items inside an IEnumerable<T> var collection = new[] { 1, 2, 3, 4, 5 }; var sequence = new[] { 2, 3 }; // IndexOf is an extension method. collection.IndexOf(sequence); // Should return 1 I wrote an IndexOf extension method for this and it works fine unless there are more than one of the first item of the sequence in collection, consecutively: // There are two items that are 2, consecutively in the collection, // which is the first item of the

Ordering Tasks by completion time while keeping track of their index in an argument list?

こ雲淡風輕ζ 提交于 2019-12-12 01:35:48
问题 I recently asked a question about the possibility of cleaning up some code, the code was meant to wait for each Task in a List<Task<T>> to complete but cancelling all Task if one returned some falsey value. A user named Servy quickly produced a method which eloquently orders a List<Task<T>> by their completion time. After reading over the answer a bit I think/thought I understand/stood the method. I then went to use the method but quickly realized an issue. I need to be able to recognize the

selecting a certain column value from looping in ienumerable

我的梦境 提交于 2019-12-12 01:24:56
问题 I have a result of IEnumerable from a stored procedure and i am looping through the results inorder to get the value of a column(GUID). I am unsure of how to go about on getting the Guid column from my results set in the foreach loop this is what i have: var results = GetGuids(instId); foreach (var item in results) { } public IEnumerable GetGuids(int id) { using (SqlCommand _command = new SqlCommand("StoredProc")) { _command.Connection = new SqlConnection(conString); _command.Connection.Open(