NUnit comparing two lists

If you’re comparing two lists, you should use test using collection constraints. Assert.That(actual, Is.EquivalentTo(expected)); Also, in your classes, you will need to override the Equals method, otherwise like gleng stated, the items in the list are still going to be compared based on reference. Simple override example: public class Example { public int ID { … Read more

convert .NET generic List to F# list

Try List.ofSeq in the Microsoft.FSharp.Collections namespace. # List.ofSeq : seq<‘T> -> ‘T list It’s not specifically for System.Collections.Generic.List<T>, but for IEnumerable<T> (seq<‘T> in F#) types in general, so it should still work. (It’s also not strictly built into the F# language, but neither is List<T> built into C# or VB.NET. Those are all part of … Read more

Replace an object in a list of objects

You have to replace the item, not the value of customListItem2. Just replace following: customListItem2 = customListItems.Where(i=> i.name == “Item 2”).First(); customListItem2 = newCustomListItem; With this: customListItem2 = customListItems.Where(i=> i.name == “Item 2”).First(); var index = customListItems.IndexOf(customListItem2); if(index != -1) customListItems[index] = newCustomListItem; Edit: As Roman R. stated in a comment, you can replace the … Read more

Fastest way to Remove Duplicate Value from a list by lambda

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 preserve the original order, but in the current implementation it will – and that’s … Read more

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

Give this a try: foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) { // do stuff here } Also please note that Type.GetProperties() has an overload which accepts a set of binding flags so you can filter out properties on a different criteria like accessibility level, see MSDN for more details: Type.GetProperties Method (BindingFlags) Last but not least … Read more

Convert an enum to List

Use Enum‘s static method, GetNames. It returns a string[], like so: Enum.GetNames(typeof(DataSourceTypes)) If you want to create a method that does only this for only one type of enum, and also converts that array to a List, you can write something like this: public List<string> GetDataSourceTypes() { return Enum.GetNames(typeof(DataSourceTypes)).ToList(); } You will need Using System.Linq; … Read more

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

I would suggest writing an extension method for this: public static IEnumerable<T> Select<T>(this IDataReader reader, Func<IDataReader, T> projection) { while (reader.Read()) { yield return projection(reader); } } You can then use LINQ’s ToList() method to convert that into a List<T> if you want, like this: using (IDataReader reader = …) { List<Customer> customers = reader.Select(r … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)