generic-list

Convert list of one type to list of another type in C# 3.5

感情迁移 提交于 2020-01-24 10:14:05
问题 I have an object, with ID and Name properties. I have a list of the objects, but I want to convert it to a list of strings containing the name of the object. I'm guessing there's some fancy Linq method that will put the name of the object into the list. List<string> myObjectNames = myObjectList.? 回答1: If you know it is specifically a List<T> and not another collection type then you can use List<T>.ConvertAll: Converts the elements in the current List<T> to another type, and returns a list

Converting a List of Base type to a List of Inherited Type

こ雲淡風輕ζ 提交于 2020-01-09 09:45:10
问题 I would be certain that this question addresses something that would have been brought up in a previous question, but I was unable to find it. There is a method in a C# class that takes as a parameter a generic List of a Base Class. I need to pass a list of an inherited class and do not know exactly how to do this. I am getting an error in my attempts. Below is sample code to illustrated this: public class A { public static void MethodC(List<A>) { // Do Something here with the list } } public

C# List<T>.BinarySearch return value when value not found

孤街浪徒 提交于 2020-01-03 08:44:11
问题 I am confused about the BinarySearch method of List<T> in case when the item does not exist. I've got List<long> theList = {1, 3, 5, ...}. theList.BInarySearch(0) returns 0, and theList.BInarySearch(3) returns 1, as expected. However, theList.BinarySearch(1) returns -2 , and not -1 as I'd expect. The MSDN manual says: "Return value: The zero-based index of item in the sorted List, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element

Merge and Update Two Lists in C#

て烟熏妆下的殇ゞ 提交于 2019-12-30 18:33:35
问题 I have two List<T> objects: For example: List 1: ID, Value where Id is populated and value is blank and it contains say IDs from 1 to 10. 1,"" 2,"" ... 10,"" List 2: ID, Value and other attributes all filled with values but this list is a subset of List 1 in terms of IDs. (e.g only 3 items) 2,67 4,90 5,98 What I want is a merged list 1, but with updated values. Does anyone have any good extension method which will do this or any elegent code to perform this operation. The final list should be

Check that value exists in a Generic List of Values

那年仲夏 提交于 2019-12-29 07:48:50
问题 I am trying to figure out how to check if testInt exists in all Car.SomeID in List So: int testInt = 10; List<Car> myCars = GetCars(); I want to see if there is a match on 10 in any of myCards.SomeID 回答1: In the more general case, with LINQ (supports any type of abstract typed list): bool hasCar = myCars.Any(c => c.SomeID == testInt); 回答2: myCars.Exists(c => c.SomeID == 10); 来源: https://stackoverflow.com/questions/1848285/check-that-value-exists-in-a-generic-list-of-values

Is List<Object> a good replacement for ArrayList?

筅森魡賤 提交于 2019-12-25 02:22:15
问题 ArrayList , which I use in legacy Compact Framework code, does not seem to be available in newfangled (.NET 4.5.1) code. I am storing instances of custom classes in it. What is a good replacement for it - List<Object> , or is there something more suitable? 回答1: As @HighCore mentioned in his comment, you should use the generic form of List, List<T> . If you have several classes defined that you need to include in that List, they probably have common properties, methods. In that case, you can

DataTemplate.DataType = Collection<Entity>?

我是研究僧i 提交于 2019-12-23 07:47:52
问题 Is there a way to create a data template that handles a list of items? I have Contact.Phones ( EntityCollection<Phone> ) and I want the data template to handle the list - add remove edit etc. Is there a way to set the DataType property of the DataTemplate to generic EntityCollection<Phone> ? 回答1: Wrap your generic list in a new class, which subclasses your generic list without adding anything. This makes it possible to bind to the list from XAML. This is described here: Can I specify a

Why does IList<T> implement IEnumerable<T> and ICollection<T> while ICollection<T> itself implements IEnumerable<T> [duplicate]

我的梦境 提交于 2019-12-21 15:42:50
问题 This question already has answers here : Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>? (4 answers) Closed 4 years ago . Why is IList defined like this? public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable public interface ICollection<T> : IEnumerable<T>, IEnumerable public interface IEnumerable<T> : IEnumerable Couldn't it just be public interface IList<T> : ICollection<T> So, to test I created these interfaces, just to be sure if that works!

Why does IList<T> implement IEnumerable<T> and ICollection<T> while ICollection<T> itself implements IEnumerable<T> [duplicate]

六眼飞鱼酱① 提交于 2019-12-21 15:42:45
问题 This question already has answers here : Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>? (4 answers) Closed 4 years ago . Why is IList defined like this? public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable public interface ICollection<T> : IEnumerable<T>, IEnumerable public interface IEnumerable<T> : IEnumerable Couldn't it just be public interface IList<T> : ICollection<T> So, to test I created these interfaces, just to be sure if that works!

Why does IList<T> implement IEnumerable<T> and ICollection<T> while ICollection<T> itself implements IEnumerable<T> [duplicate]

和自甴很熟 提交于 2019-12-21 15:42:35
问题 This question already has answers here : Why does List<T> implement IList<T>, ICollection<T> and IEnumerable<T>? (4 answers) Closed 4 years ago . Why is IList defined like this? public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable public interface ICollection<T> : IEnumerable<T>, IEnumerable public interface IEnumerable<T> : IEnumerable Couldn't it just be public interface IList<T> : ICollection<T> So, to test I created these interfaces, just to be sure if that works!