Posts

Showing posts with the label List<T>

Donate

How To Remove Duplicate Items In A List<T> Using IEqualityComparer<T>

Image
Good evening. Here's a simple demo on how to remove duplicate items in a List object using IEqualityComparer given that the generic list's type is complex. First we setup a simple model Employee class with Age, Name and Address properties. class Employee { public int Age { get ; set ; } public string Name { get ; set ; } public string Address { get ; set ; } } Next is to create a comparer class that implements IEqualityComparer interface. class EmployeeComparer : IEqualityComparer<Employee> { public bool Equals(Employee emp1, Employee emp2) { if (Object.ReferenceEquals(emp1, emp2)) return true ; if (Object.ReferenceEquals(emp1, null ) || Object.ReferenceEquals(emp2, null )) return false ; return (emp1.Age == emp2.Age) && (emp1.Name == emp2.Name) && (emp1.Address == emp2.Address); } public int GetHashCode(Employee obj) {

AllowUsersToAddRows In DataGridView Not Working If DataSource is List<T>

Greetings! Going back to a previous project of mine, I found out that some DataGridView control's DataSource where set using List<T> and as a result, prevented the users to add new data to the DataGridView. private void List() { List<Item> list = new List<Item>(); for ( int i = 0; i < 100; i++) { list.Add( new Item() { ID = i, Name = String.Format( "{0}:{1}" , "Test" , i.ToString()) }); } DataGridView1.DataSource = list; } The common solution is to use DataTable as the DataSource but if we want to use a List type object, we could use BindingList<T> or BindingSource. Both of these have AllowNew property which indicates that you can add items to the list using the AddNew() method. It is stated in DataGridView.AllowUsersToAddRows that " If the DataGridView is bound to data, the user is allowed to add rows if both this property and the data source's IBindingList.AllowNew property are set to true. ".Well, Lis

How To Convert DataTable To List Using LINQ And C#

Hello, The code below converts a DataTable object to generic List object using LINQ and C#. It assumes that the DataTable columns match with class properties. C# Code 1 2 3 4 5 6 7 8 var personEnumerable = table.AsEnumerable(); List<Person> ListPosition = new List<Person>(); ListPosition = ( from item in personEnumerable select new Person { ID = item.Field< int >( "ID" ).ToString(), Salary = item.Field< double >( "Salary" ).ToString() }).ToList(); Class 1 2 3 4 5 public class Person { public string ID { get ; set ; } public string Salary { get ; set ; } }

Predicate Wrapper Custom Class in C#

Based from this post: Predicate Wrapper Custom Class in VB.NET , here's the equivalent classes in C#. FindTrack class: public class SystemTrack { public int ID { get ; set ; } public string Author { get ; set ; } public string Title { get ; set ; } public static bool FindIndexTrackNumber(SystemTrack systrac, int searchArg) { return systrac.ID.Equals(searchArg); } } Predicate Class: public delegate bool PredicateWrapperDelegate<T, A>(T item, A argument); public class PredicateWrapper <T, A> { private A _argument; private PredicateWrapperDelegate<T, A> _wrapperDelegate; public PredicateWrapper(A argument, PredicateWrapperDelegate<T, A> wrapperDelegate) { _argument = argument; _wrapperDelegate = wrapperDelegate; } private bool InnerPredicate(T item) { return

Predicate Wrapper Custom Class in VB.NET

Here's a custom predicate wrapper class I got from visual basic forums. I'll be converting and posting the C# equivalent for this. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Public Delegate Function PredicateWrapperDelegate( Of T, A) _ ( ByVal item As T, ByVal argument As A) As Boolean Public Class PredicateWrapper ( Of T, A) Private _argument As A Private _wrapperDelegate As PredicateWrapperDelegate( Of T, A) Public Sub New( ByVal argument As A, _ ByVal wrapperDelegate As PredicateWrapperDelegate( Of T, A)) _argument = argument _wrapperDelegate = wrapperDelegate End Sub Private Function InnerPredicate( ByVal item As T) As Boolean Return _wrapperDelegate(item, _argument) End Function Public Shared Widening Operator CType ( _ ByVal wrapper As PredicateWrapper( Of T, A)) _ As Predicate( Of T) Return New Predicate( Of T

Sorting List<T> Generic Collection String In C#

This example uses List generic collection and will sort names in descending order. Names with New will be displayed on top of the list. This is applicable if you will be displaying products names with NEW in it's product name as recently displayed. List< string > Names = new List< string >(); private void FListSort_Load( object sender, EventArgs e) { Names.Add( "Mike" ); Names.Add( "John" ); Names.Add( "Titch" ); Names.Add( "Harold" ); Names.Add( "Klent" ); Names.Add( "Thomas New" ); Names.Add( "Mary" ); Names.Add( "Fultron New" ); Names.Add( "Khayce" ); Names.Add( "Tim" ); Names.Add( "Joker New" ); Names.Add( "Linda" ); Names.Add( "Arthur New" ); Names.Add( "Baby Lee Jones"

Donate