Consider an entity named Employee which contains id,age and name as properties
I have two lists containing the Employee details
I have to compare the two lists excluding the id column
Please help with your suggestions
This will yield all the entries that are the same in both lists, ignoring the Id Property of your Employee:
var employees1 = new List<Employee>
{
new Employee(1, "Thomas", 12),
new Employee(2, "Alex", 24),
new Employee(3, "Tobias", 13),
new Employee(4, "Joshua", 12),
new Employee(5, "Thomas", 24)
};
var employees2 = new List<Employee>
{
new Employee(1, "Thomas", 12),
new Employee(2, "Yu", 24),
new Employee(3, "Max", 13),
new Employee(4, "Joshua", 30),
new Employee(5, "Maico", 13)
};
var duplicates = employees1.Intersect(employees2, new EmployeeComparer());
class EmployeeComparer : IEqualityComparer<Employee>
{
public bool Equals(Employee employee1, Employee employee2)
{
if (Object.ReferenceEquals(employee1, null) || Object.ReferenceEquals(employee2, null) ||
Object.ReferenceEquals(employee1, employee2)) return false;
return employee1.Name == employee2.Name && employee1.Age == employee2.Age;
}
public int GetHashCode(Employee employee)
{
return 0;
}
}
class Employee
{
public Employee(int id, string name, int age)
{
Id = id;
Name = name;
Age = age;
}
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
As the post is tagged with LINQ I have used that in my answer.
static void Main(string[] args)
{
var list1 = new List<Person>();
var list2 = new List<Person>();
list1.Add(new Person(1, "james", "moon"));
list1.Add(new Person(1, "bob", "bar"));
list1.Add(new Person(1, "tim", "lane"));
list1.Add(new Person(1, "fizz", "sea"));
list2.Add(new Person(1, "buzz", "space"));
list2.Add(new Person(1, "james", "moon"));
var result = findDuplicates(list1, list2);
}
public static List<Person> findDuplicates(List<Person> l1, List<Person> l2)
{
return l1.Where(p => l2.Any(z => z.FName == p.FName && z.Addre == p.Addre)).ToList();
}
Person Class
public class Person
{
private int id;
private string fName;
private string addre;
public string Addre
{
get { return addre; }
set { addre = value; }
}
public string FName
{
get { return fName; }
set { fName = value; }
}
public int ID
{
get { return id; }
set { id = value; }
}
public Person(int i, string f, string a)
{
ID = i;
FName = f;
Addre = a;
}
}
Assuming Employee class:
class Employee
{
public int id { get; set; }
public int age { get; set; }
public string name { get; set; }
}
You can simply use Intersect :
var list1 = new List<Employee> {
new Employee{ id=2 , age=23, name="Hari"},
new Employee{ id=3 , age=10, name="Joe"},
new Employee{ id=4 , age=29, name="Daniel"},
};
var list2 = new List<Employee> {
new Employee{ id=1 , age=23, name="Hari"},
new Employee{ id=5 , age=10, name="Joe"},
new Employee{ id=6 , age=29, name="Daniel"},
};
var intersect = list1.Select(e => new { e.age, e.name }).Intersect(list2.Select(e => new { e.age, e.name })).ToList();
Related
I have a list that basically look like this...
public class Area
{
public int Id { get; set; }
public string Name { get; set; }
public List<ZipCodeAdresses> ListOfIncludedDestinations { get; set; }
}
public class ZipCodeAdresses
{
public int AreaId { get; set; }
public List<Person> AdressList { get; set; }
}
public class Person
{
public string MottagarNamn { get; set; }
public string Street { get; set; }
}
var intListToRemove = new List<int>(){2,3};
var list = new List<Area>();
var subList = new List<ZipCodeAdresses>();
var personList = new List<Person>
{
new Person() {MottagarNamn = "User 1"},
new Person() {MottagarNamn = "User 2"}
};
subList.Add(new ZipCodeAdresses(){AdressList = personList , AreaId = 1});
personList = new List<Person>
{
new Person() {MottagarNamn = "User 3"},
new Person() {MottagarNamn = "User 4"}
};
subList.Add(new ZipCodeAdresses() { AdressList = personList, AreaId = 2 });
list.Add(new Area(){Name = "List A", ListOfIncludedDestinations = subList});
subList = new List<ZipCodeAdresses>();
personList = new List<Person>
{
new Person() {MottagarNamn = "User 5"},
new Person() {MottagarNamn = "User 6"}
};
subList.Add(new ZipCodeAdresses() { AdressList = personList, AreaId = 3 });
personList = new List<Person>
{
new Person() {MottagarNamn = "User 7"},
new Person() {MottagarNamn = "User 8"}
};
subList.Add(new ZipCodeAdresses() { AdressList = personList, AreaId = 4 });
list.Add(new Area() { Name = "List B", ListOfIncludedDestinations = subList });
I need to be able to remove from the list ListOfIncludedDestinations where AreaId is equal to any integer in intListToRemove which in this example is 2 and 3?
List<T> contains a method RemoveAll, that removes all entries that fulfill a certain condition. In your case it is:
foreach(var entry in list)
{
entry.ListOfIncludedDestinations.RemoveAll(x => intListToRemove.Contains(x.AreaId));
}
This loops through your list, and for every entry it removes all entries in ListOfIncludedDestinations that have an AreadId which is in intListToRemove.
Online demo: https://dotnetfiddle.net/ialnPb
You should add this sample code to remove them from the list :
foreach (var i in list)
i.ListOfIncludedDestinations.RemoveAll(o => intListToRemove.Contains(o.AreaId));
The best way I can describe what I'm trying to do is "Nested DistinctBy".
Let's say I have a collection of objects. Each object contains a collection of nicknames.
class Person
{
public string Name { get; set; }
public int Priority { get; set; }
public string[] Nicknames { get; set; }
}
public class Program
{
public static void Main()
{
var People = new List<Person>
{
new Person { Name = "Steve", Priority = 4, Nicknames = new string[] { "Stevo", "Lefty", "Slim" }},
new Person { Name = "Karen", Priority = 6, Nicknames = new string[] { "Kary", "Birdie", "Snookie" }},
new Person { Name = "Molly", Priority = 3, Nicknames = new string[] { "Mol", "Lefty", "Dixie" }},
new Person { Name = "Greg", Priority = 5, Nicknames = new string[] { "G-man", "Chubs", "Skippy" }}
};
}
}
I want to select all Persons but make sure nobody selected shares a nickname with another. Molly and Steve both share the nickname 'Lefty' so I want to filter one of them out. Only the one with highest priority should be included. If there is a highest priority tie between 2 or more then just pick the first one of them. So in this example I would want an IEnumerable of all people except Steve.
EDIT: Here's another example using music album instead of person, might make more sense.
class Album
{
string Name {get; set;}
int Priority {get;set;}
string[] Aliases {get; set;}
{
class Program
{
var NeilYoungAlbums = new List<Album>
{
new Person{ Name = "Harvest (Remastered)", Priority = 4, Aliases = new string[] { "Harvest (1972)", "Harvest (2012)"}},
new Person{ Name = "On The Beach", Priority = 6, Aliases = new string[] { "The Beach Album", "On The Beach (1974)"}},
new Person{ Name = "Harvest", Priority = 3, Aliases = new string[] { "Harvest (1972)"}},
new Person{ Name = "Freedom", Priority = 5, Aliases = new string[] { "Freedom (1989)"}}
};
}
The idea here is we want to show his discography but we want to skip quasi-duplicates.
I would solve this using a custom IEqualityComparer<T>:
class Person
{
public string Name { get; set; }
public int Priority { get; set; }
public string[] Nicknames { get; set; }
}
class PersonEqualityComparer : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
if (x == null || y == null) return false;
return x.Nicknames.Any(i => y.Nicknames.Any(j => i == j));
}
// This is bad for performance, but if performance is not a
// concern, it allows for more readability of the LINQ below
// However you should check the Edit, if you want a truely
// LINQ only solution, without a wonky implementation of GetHashCode
public int GetHashCode(Person obj) => 0;
}
// ...
var people = new List<Person>
{
new Person { Name = "Steve", Priority = 4, Nicknames = new[] { "Stevo", "Lefty", "Slim" } },
new Person { Name = "Karen", Priority = 6, Nicknames = new[] { "Kary", "Birdie", "Snookie" } },
new Person { Name = "Molly", Priority = 3, Nicknames = new[] { "Mol", "Lefty", "Dixie" } },
new Person { Name = "Greg", Priority = 5, Nicknames = new[] { "G-man", "Chubs", "Skippy" } }
};
var distinctPeople = people.OrderBy(i => i.Priority).Distinct(new PersonEqualityComparer());
EDIT:
Just for completeness, this could be a possible LINQ only approach:
var personNicknames = people.SelectMany(person => person.Nicknames
.Select(nickname => new { person, nickname }));
var groupedPersonNicknames = personNicknames.GroupBy(i => i.nickname);
var duplicatePeople = groupedPersonNicknames.SelectMany(i =>
i.OrderBy(j => j.person.Priority)
.Skip(1).Select(j => j.person)
);
var distinctPeople = people.Except(duplicatePeople);
A LINQ-only solution
var dupeQuery = people
.SelectMany( p => p.Nicknames.Select( n => new { Nickname = n, Person = p } ) )
.ToLookup( e => e.Nickname, e => e.Person )
.SelectMany( e => e.OrderBy( p => p.Priority ).Skip( 1 ) );
var result = people.Except( dupeQuery ).ToList();
See .net fiddle sample
This works once, then you have to clear the set. Or store the results in a collection.
var uniqueNicknames = new HashSet<string>();
IEnumerable<Person> uniquePeople = people
.OrderBy(T => T.Priority) // ByDescending?
.Where(T => T.Nicknames.All(N => !uniqueNicknames.Contains(N)))
.Where(T => T.Nicknames.All(N => uniqueNicknames.Add(N)));
I have a Linq question: (DotNet Framework 4.0)
I have the following classes:
public class Employee
{
public Guid? EmployeeUUID { get; set; }
public string SSN { get; set; }
}
public class JobTitle
{
public Guid? JobTitleSurrogateKey { get; set; }
public string JobTitleName { get; set; }
}
public class EmployeeToJobTitleMatchLink
{
public EmployeeToJobTitleMatchLink()
{
this.TheJobTitle = new JobTitle() { JobTitleSurrogateKey = Guid.NewGuid(), JobTitleName = "SomeJobTitle:" + Guid.NewGuid().ToString("N") };
}
public Guid LinkSurrogateKey { get; set; }
/* Related Objects */
public Employee TheEmployee { get; set; }
public JobTitle TheJobTitle { get; set; }
}
public class Organization
{
public Organization()
{
this.Links = new List<EmployeeToJobTitleMatchLink>();
}
public int OrganizationSurrogateKey { get; set; }
public ICollection<EmployeeToJobTitleMatchLink> Links { get; set; }
}
In my code below, I can compare 2 child-collections and get the results I need (in "matches1".
Here I am using the "SSN" string property to compare and find the overlaps. And the Console.Write for matches1 works as I expect.
What I don't know how to do is compare the first child collection (org10) to all the children in (allOtherOrgsExceptOrg10 (all the Organizations and all the Links of these Organizations )
The commented out code shows kinda what I'm trying to do, one of my many feeble attempts today.
But basically, match2 would be populated with all the SSN overlaps...but comparing org10 with allOtherOrgsExceptOrg10, all their "Links", and their Employee.SSN's.
org10 overlaps with org20 with "AAA", so match2 would contain "AAA". and org10 overlaps with org30 with "BBB" so match2 would contain "BBB".
Organization org10 = new Organization();
org10.OrganizationSurrogateKey = 10;
Employee e11 = new Employee() { SSN = "AAA", EmployeeUUID = new Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") };
EmployeeToJobTitleMatchLink link11 = new EmployeeToJobTitleMatchLink();
link11.TheEmployee = e11;
org10.Links.Add(link11);
Employee e12 = new Employee() { SSN = "BBB", EmployeeUUID = new Guid("BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB") };
EmployeeToJobTitleMatchLink link12 = new EmployeeToJobTitleMatchLink();
link12.TheEmployee = e12;
org10.Links.Add(link12);
Organization org20 = new Organization();
org20.OrganizationSurrogateKey = 20;
Employee e21 = new Employee() { SSN = "AAA", EmployeeUUID = new Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA") };
EmployeeToJobTitleMatchLink link21 = new EmployeeToJobTitleMatchLink();
link21.TheEmployee = e21;
org20.Links.Add(link21);
Employee e22 = new Employee() { SSN = "CCC", EmployeeUUID = new Guid("CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCCC") };
EmployeeToJobTitleMatchLink link22 = new EmployeeToJobTitleMatchLink();
link22.TheEmployee = e22;
org20.Links.Add(link22);
Organization org30 = new Organization();
org30.OrganizationSurrogateKey = 30;
Employee e31 = new Employee() { SSN = "BBB", EmployeeUUID = new Guid("BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB") };
EmployeeToJobTitleMatchLink link31 = new EmployeeToJobTitleMatchLink();
link31.TheEmployee = e31;
org30.Links.Add(link31);
Employee e32 = new Employee();
e32.SSN = "ZZZ";
EmployeeToJobTitleMatchLink link32 = new EmployeeToJobTitleMatchLink();
link32.TheEmployee = e32;
org30.Links.Add(link32);
IList<Organization> allOtherOrgsExceptOrg10 = new List<Organization>();
/* Note, I did not add org10 here */
allOtherOrgsExceptOrg10.Add(org20);
allOtherOrgsExceptOrg10.Add(org30);
IEnumerable<EmployeeToJobTitleMatchLink> matches1 =
org10.Links.Where(org10Link => org20.Links.Any(org20Link => org20Link.TheEmployee.SSN.Equals(org10Link.TheEmployee.SSN, StringComparison.OrdinalIgnoreCase)));
IEnumerable<EmployeeToJobTitleMatchLink> matches2 = null;
//org10.Links.Where(org10Link => ( allOtherOrgs.Where ( anyOtherOrg => anyOtherOrg.Links.Any(dbSideChild => dbSideChild.TheEmployee.SSN == org10Link.TheEmployee.SSN)) );
if (null != matches1)
{
foreach (EmployeeToJobTitleMatchLink link in matches1)
{
Console.WriteLine(string.Format("matches1, SSN = {0}", link.TheEmployee.SSN));
}
}
if (null != matches2)
{
foreach (EmployeeToJobTitleMatchLink link in matches2)
{
Console.WriteLine(string.Format("matches2, SSN = {0}", link.TheEmployee.SSN));
}
}
matches2 =
allOtherOrgsExceptOrg10.SelectMany(x => x.Links)
.Where(x => org10.Links.Select(o => o.TheEmployee.SSN).Contains(x.TheEmployee.SSN));
You can use the SelectMany on the allOther collection to select all Links over all org's. Then check if any SSN is inside the org10 List.
See: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.selectmany(v=vs.100).aspx
You can use SelectMany to flatten out the collection and then use it just like you have for matches1
IEnumerable<EmployeeToJobTitleMatchLink> matches2 =
org10.Links.Where(
org10Link =>
allOtherOrgsExceptOrg10.SelectMany(allOtherOrgs => allOtherOrgs.Links).Any(
anyOtherLink =>
anyOtherLink.TheEmployee.SSN.Equals(org10Link.TheEmployee.SSN, StringComparison.OrdinalIgnoreCase)));
The SelectMany will make it seem like one IEnumerable instead of and IEnumerable of an IEnumerable.
How to print out all the persons and their pets, using Linq. I only want to print out persons who have pets.
Prefer result be like:
Kate Bed:
Rex
Sally
My not working solution is here:
class Program
{
static void Main(string[] args)
{
result();
}
static void result() {
var list = StaticGenator.getPersons().Where(x => x.Pets != null);
foreach (var person in list)
{
Console.WriteLine(person.Firstname + " " + person.Lastname + ":");
foreach(var pet in list){
Console.WriteLine(" " + pet.Pets);
}
}
}
What i get is:
Kate Bed:
system.collection.generic.list'1[MainLibrary.Pet]
system.collection.generic.list'1[MainLibrary.Pet]
Here is the code to understand what I am asking:
Data is held here:
public static class StaticGenator
{
public static List<Person> getPersons()
{
List<Person> persons = new List<Person>();
persons.Add(new Person() { Firstname = "Sam", Lastname = "Car", BirthDate = new DateTime(2001, 01, 01), PersonId = 1, Sex = Sex.Man });
persons.Add(new Person() { Firstname = "Kate", Lastname = "Bed", BirthDate = new DateTime(1995, 11, 11), PersonId = 2, Sex = Sex.Woman, Pets = new List<Pet>() { new Pet { Firstname = "Rex", BirthDate = new DateTime(2007, 1, 1), Sex = Sex.Man, PetId = 1 }, new Pet { Firstname = "Sally", BirthDate = new DateTime(2004, 2, 1), Sex = Sex.Woman, PetId = 2 } } });
return persons;
}
}
Person Class:
public class Person
{
public int PersonId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public DateTime BirthDate { get; set; }
public Sex Sex{ get; set; }
public int Age {
get
{
var age= DateTime.Now.Year - BirthDate.Year;
if (DateTime.Now.Day >= BirthDate.Day && DateTime.Now.Month >= BirthDate.Month)
return age;
else
return age- 1;
}
}
public List<Pet> Pets { get; set; }
}
Pet Class:
public class Pet
{
public int PetId { get; set; }
public String Firstname { get; set; }
public Sex Sex { get; set; }
public DateTime BirthDate { get; set; }
public int Age { get; set; }
}
Sex enum:
public enum Sex{
Man,
Woman
}
foreach (var person in list)
{
Console.WriteLine(person.Firstname + " " + person.Lastname + ":");
foreach(var pet in person.Pets) // iterate over Pets of person
{
Console.WriteLine(" " + pet.Firstname); // write pet's name
}
}
Keep in mind - you can have problem if somebody will add null pet to pets collection or if there is empty pets list. So, probably correct query to get persons with pets is:
var peopleWithPets = from p in StaticGenator.getPersons()
where p.Pets != null &&
p.Pets.Any() &&
p.Pets.All(x => x != null)
select p;
Also use string formatting:
foreach (var person in peopleWithPets)
{
Console.WriteLine("{0} {1}:", person.Firstname, person.Lastname);
foreach(var pet in person.Pets)
Console.WriteLine("\t{0}", pet.Firstname);
}
Also I suggest you to follow Capitalization Styles recommended by MicroSoft.
foreach(var pet in list)
{
Console.WriteLine(" " + pet.Pets);
}
Should be:
foreach(var pet in person.Pets)
{
Console.WriteLine(" " + pet.FirstName);
}
You have to iterate over the pet collection of the current user that is being iterated.
Suppose you have some list (you mentioned above) with your datastructure
List<Person> persons = new List<Person>();
persons.Add(new Person() { Firstname = "Sam", Lastname = "Car", BirthDate = new DateTime(2001, 01, 01), PersonId = 1, Sex = Sex.Man });
persons.Add(new Person() { Firstname = "Kate", Lastname = "Bed", BirthDate = new DateTime(1995, 11, 11), PersonId = 2, Sex = Sex.Woman, Pets = new List<Pet>() { new Pet { Firstname = "Rex", BirthDate = new DateTime(2007, 1, 1), Sex = Sex.Man, PetId = 1 }, new Pet { Firstname = "Sally", BirthDate = new DateTime(2004, 2, 1), Sex = Sex.Woman, PetId = 2 } } });
You can very easily filter as well as write to console with these 4 lines of code
persons.Where(p => p.Pets != null && p.Pets.Any()).ToList().ForEach(p =>
{
Console.WriteLine(p.Firstname + " " + p.Lastname + "\n");
p.Pets.ForEach(pt => Console.WriteLine(pt.Firstname));
});
var output = String.Join("\n", persons.Select(person => $"{person.Firstname} {person.Lastname}"));
Output:
Sam Car
Kate Bed
I have this code:
void Main()
{
List<Employee> employeeList;
employeeList = new List<Employee>
{
{new Employee("000001", "DELA CRUZ, JUAN T.")},
{new Employee("000002", "GOMEZ, MAR B.")},
{new Employee("000003", "RIVERA, ERWIN J.")}
};
employeeList.Dump();
}
public class Employee
{
public string EmployeeNo { get; set; }
public string Name { get; set; }
public Employee(string employeeNo, string name)
{
this.EmployeeNo = employeeNo;
this.Name = name;
}
}
How should I make a new instance of Employee class using the properties only and add that instance to the employeeList (I mean not using the class constructor of employee)?
I already made a solution but it's too lengthy. How should I shorten it?
void Main()
{
List<Employee> employeeList;
#region - I want to shorten these lengthy codes.
Employee employee1 = new Employee();
employee1.EmployeeNo = "000001";
employee1.Name = "DELA CRUZ, JUAN T.";
Employee employee2 = new Employee();
employee2.EmployeeNo = "000002";
employee2.Name = "GOMEZ, MAR B.";
// other employees...
#endregion
employeeList = new List<Employee>
{
employee1,
employee2
};
employeeList.Dump();
}
public class Employee
{
public string EmployeeNo { get; set; }
public string Name { get; set; }
}
You could do this
var list = new List<Employee>
{
new Employee {EmployeeNo = "000001", Name = "Peter Pan"},
new Employee {EmployeeNo = "000002", Name = "King Kong"}
};
of this
public class EmployeeList : List<Employee>
{
public void Add(string no, string name)
{
this.Add(new Employee(no, name));
}
}
var list = new EmployeeList
{
{ "000001", "Peter Pan" },
{ "000002", "King Kong"}
};
You could do something like that
var employee1 = New Employee() { EmployeeNo = "000001", Name = "DELA CRUZ, JUAN T." };
How about this?
void Main()
{
var employeeList = new List<Employee> {
new Employee { EmployeeNo = "000001", Name = "DELA CRUZ, JUAN T." },
new Employee { EmployeeNo = "000002", Name = "GOMEZ, MAR B." }
};
employeeList.Dump();
}
public class Employee
{
public string EmployeeNo { get; set; }
public string Name { get; set; }
}