generic-list

Saving from List<T> to txt

微笑、不失礼 提交于 2019-11-26 16:39:53
问题 I want my program to read from two text files into one List<T> . The List<T> is sorting and cleaning duplicates. I want the List<T> to save (after sorting and cleaning) to a txt file. But when I looked in the result txt file, I found this message: System.Collections.Generic.List`1[System.String] Does anyone have an idea how I could fix this error? Here is my code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Uniqpass { class

Fastest way to Remove Duplicate Value from a list<> by lambda

半城伤御伤魂 提交于 2019-11-26 12:06:20
问题 what is fastest way to remove duplicate values from a list. Assume List<long> longs = new List<long> { 1, 2, 3, 4, 3, 2, 5 }; So I am interesting in use lambda to remove duplicate and returned : {1, 2, 3, 4, 5} . What is your suggestion? 回答1: The easiest way to get a new list would be: List<long> unique = longs.Distinct().ToList(); Is that good enough for you, or do you need to mutate the existing list? The latter is significantly more long-winded. Note that Distinct() isn't guaranteed to

How to make correct clone of the List<MyObject>? [duplicate]

喜夏-厌秋 提交于 2019-11-26 11:29:35
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I clone a generic list in C#? List<MyObject> a1 = new List<MyObject>(); var new1 = a1; Now if I change a1 then new1 is going to be changed as well. So my question is how to make a clone of a1 correctly? 回答1: This wont Clone each item in the list but will create you a new list var new1 = new List<MyObject>(a1); If you want to clone each Item in the list you can implement ICloneable on MyObject var new1 =

C# generic list <T> how to get the type of T? [duplicate]

雨燕双飞 提交于 2019-11-26 11:15:06
This question already has an answer here: How to get the type of T from a member of a generic class or method? 16 answers I’m working on a reflection project, and now I’m stuck. If I have an object of “myclass” that can hold a List does anyone know how to get the type as in the code below if the property myclass.SomList is empty? List<myclass> myList = dataGenerator.getMyClasses(); lbxObjects.ItemsSource = myList; lbxObjects.SelectionChanged += lbxObjects_SelectionChanged; private void lbxObjects_SelectionChanged(object sender, SelectionChangedEventArgs e) { Reflect(); } Private void Reflect()

c# foreach (property in object)… Is there a simple way of doing this?

我的未来我决定 提交于 2019-11-26 09:18:04
问题 I have a class containing several properties (all are strings if it makes any difference). I also have a list, which contains many different instances of the class. While creating some unit tests for my classes I decided I wanted to loop through each object in the list and then loop through each property of that object... I thought doing this would be as simple as... foreach (Object obj in theList) { foreach (Property theProperties in obj) { do some stufff!!; } } But this didnt work! :( I get

Lists with wildcards cause Generic voodoo error

荒凉一梦 提交于 2019-11-26 09:00:32
问题 Does anyone know why the following code does not compile? Neither add() nor addAll() works as expected. Removing the \"? extends\" part makes everything work, but then I would not be able to add subclasses of Foo. List<? extends Foo> list1 = new ArrayList<Foo>(); List<? extends Foo> list2 = new ArrayList<Foo>(); /* Won\'t compile */ list2.add( new Foo() ); //error 1 list1.addAll(list2); //error 2 error 1: IntelliJ says: add(capture<? extends Foo>) in List cannot be applied to add(Foo) The

Required Attribute on Generic List Property

回眸只為那壹抹淺笑 提交于 2019-11-26 05:39:11
问题 Is it possible to put a [Required] attribute onto a List<> property? I bind to a generic list on POST and was wondering if I could make ModelState.IsValid() fail if the property has 0 items in it? 回答1: Adding the Required attribute to a list-style property doesn't really do what you want. The will complain if the list isn't created, but won't complain if the list exists with 0 item in it. However, it should be easy enough to derive your own data annotations attribute and make it check the

C# generic list <T> how to get the type of T? [duplicate]

瘦欲@ 提交于 2019-11-26 02:07:53
问题 This question already has an answer here: How to get the type of T from a member of a generic class or method? 16 answers I’m working on a reflection project, and now I’m stuck. If I have an object of “myclass” that can hold a List does anyone know how to get the type as in the code below if the property myclass.SomList is empty? List<myclass> myList = dataGenerator.getMyClasses(); lbxObjects.ItemsSource = myList; lbxObjects.SelectionChanged += lbxObjects_SelectionChanged; private void

Remove duplicates in the list using linq

房东的猫 提交于 2019-11-26 00:06:59
问题 I have a class Items with properties (Id, Name, Code, Price) . The List of Items is populated with duplicated items. For ex.: 1 Item1 IT00001 $100 2 Item2 IT00002 $200 3 Item3 IT00003 $150 1 Item1 IT00001 $100 3 Item3 IT00003 $150 How to remove the duplicates in the list using linq? 回答1: var distinctItems = items.Distinct(); To match on only some of the properties, create a custom equality comparer, e.g.: class DistinctItemComparer : IEqualityComparer<Item> { public bool Equals(Item x, Item y

How can I easily convert DataReader to List<T>? [duplicate]

岁酱吖の 提交于 2019-11-25 23:45:51
问题 This question already has an answer here: Improve data access layer select method Pattern 7 answers Convert rows from a data reader into typed results 8 answers I have data in a DataReader which I want to be converted to a List<T> . What is a possible simple solution for this? For e.g. in CustomerEntity class, I have CustomerId and CustomerName properties.If my DataReader returns these two columns as data, then how can I convert it into List<CustomerEntity> . 回答1: I have seen systems that use