ienumerable

I need to iterate and count. What is fastest or preferred: ToArray() or ToList()? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-19 12:29:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is it better to call ToList() or ToArray() in LINQ queries? I have code like this: void Foobar(string[] arr, Dictionary<string, string[]>) { var t = arr.Intersect(dic.Keys).ToList(); // .or ToArray() ? foreach(var item in t) { .. } var j = t.Count; // also I need this } which method is preferred? I could go without any but I need to know the size and I don't want to call Enumerable.Count<T>() - it seems do do

How can I create a singleton IEnumerable?

孤街浪徒 提交于 2019-12-19 12:28:12
问题 Does C# offer some nice method to cast a single entity of type T to IEnumerable<T> ? The only way I can think of is something like: T entity = new T(); IEnumerable<T> = new List { entity }.AsEnumerable(); And I guess there should be a better way. 回答1: Your call to AsEnumerable() is unnecessary. AsEnumerable is usually used in cases where the target object implements IQueryable<T> but you want to force it to use LINQ-to-Objects (when doing client-side filtering on a LINQ-compatible ORM, for

Using Linq to pull from IEnumerable<XElement> and store in data table

不羁岁月 提交于 2019-12-19 11:36:12
问题 I have a datatable that looks like the following public static DataTable SetColumnHeaders(DataTable KeyDataTable) { KeyDataTable.Columns.Add("First_Name", typeof(string)); KeyDataTable.Columns.Add("Last_Name", typeof(string)); KeyDataTable.Columns.Add("Address1", typeof(string)); KeyDataTable.Columns.Add("Address2", typeof(bool)); KeyDataTable.Columns.Add("City", typeof(string)); KeyDataTable.Columns.Add("State", typeof(bool)); KeyDataTable.Columns.Add("Zip", typeof(string)); KeyDataTable

C# how to convert IEnumerable anonymous lists into data table

断了今生、忘了曾经 提交于 2019-12-19 10:05:22
问题 Many solutions are available that convert lists to DataTable that use reflection, and that would work for converting anonymous types. However, it there are lots of lists of anonymous types, then performance can be an issue. Is this the only way to create a DataTable from a list? Are there faster ways of doing this? 回答1: It would absolutely be better to do this using proper named POCO/DTO/etc classes, but it can still be done. The cost of reflection can be removed by using meta-programming,

C# foreach on IEnumerable vs. List - element modification persistent only for array - Why?

a 夏天 提交于 2019-12-19 09:57:26
问题 In C#, I have noticed that if I am running a foreach loop on a LINQ generated IEnumerable<T> collection and try to modify the contents of each T element, my modifications are not persistent. On the other hand, if I apply the ToArray() or ToList() method when creating my collection, modification of the individual elements in the foreach loop are persistent. I suspect that this is in some way related to deferred execution, but exactly how is not entirely obvious to me. I would really appreciate

C# Cannot convert from IEnumerable<Base> to IEnumerable<Derived>

◇◆丶佛笑我妖孽 提交于 2019-12-19 07:39:09
问题 I recently run into trouble when trying to AddRange(IEnumerable) to a List. Probably a classic issue, but I do not really get it yet. I understand that methods expecting a List parameter are not satisfied with a List, because they might try to add a Base to the List, which is obviously impossible. But if i get this correctly, since IEnumerables themselves cannot be changed, it ought to work in this case. The code i thought of looks like this: class Foo { } class Bar : Foo { } class FooCol {

Using IReadOnlyCollection<T> instead of IEnumerable<T> for parameters to avoid possible multiple enumeration

你说的曾经没有我的故事 提交于 2019-12-19 05:35:08
问题 My question is related to this one concerning the use of IEnumerable<T> vs IReadOnlyCollection<T> . I too have always used IEnumerable<T> to expose collections as both return types and parameters because it benefits from being both immutable and lazily executed. However, I am becoming increasingly concerned about the proliferation of places in my code where I must enumerate a parameter to avoid the possible multiple enumeration warning that ReSharper gives. I understand why ReSharper suggests

Using IReadOnlyCollection<T> instead of IEnumerable<T> for parameters to avoid possible multiple enumeration

耗尽温柔 提交于 2019-12-19 05:35:08
问题 My question is related to this one concerning the use of IEnumerable<T> vs IReadOnlyCollection<T> . I too have always used IEnumerable<T> to expose collections as both return types and parameters because it benefits from being both immutable and lazily executed. However, I am becoming increasingly concerned about the proliferation of places in my code where I must enumerate a parameter to avoid the possible multiple enumeration warning that ReSharper gives. I understand why ReSharper suggests

Is order of dependencies guaranteed when injecting IEnumerable<T>

五迷三道 提交于 2019-12-19 05:23:41
问题 I register in container services implementing IMyService. Do I have any guarantees about their order in container.Resolve<IEnumerable<IMyService>> ? 回答1: No, there's no ordering guaranteed here. We've considered extensions to enable it but for now it's something to handle manually. 回答2: Just as extra help for people like me landing on this page... Here is an example how one could do it. public static class AutofacExtensions { private const string OrderString = "WithOrderTag"; private static

What is the best wayto add single element to an IEnumerable collection?

爱⌒轻易说出口 提交于 2019-12-19 05:21:44
问题 I'm surprised to see that there does not appear to be a method to add a single element to an IEnumerable collection. How can I add a single element to an IEnumerable collection? 回答1: You can't really add elements to an IEnumerable as it's supposed to be read only. Your best bet is either something like: return new List<Foo>(enumerable){ new Foo() }; or return enumerable.Concat(new [] { new Foo() }); 回答2: The point of IEnumerable is that it's a readonly pull style collection. If you want, you