score:1
The idea is choosing first element and then removing corresponding elements from list to make sure next choice is unique, as below:
var rankings = new List<Rankings> {
new Rankings{ JOB_ID= 4,EMPLOYEE_ID= 3, OVERALL_SCORE= 800 },
new Rankings{ JOB_ID= 4,EMPLOYEE_ID= 4, OVERALL_SCORE= 800 },
new Rankings{ JOB_ID= 3,EMPLOYEE_ID= 1, OVERALL_SCORE= 800 },
new Rankings{ JOB_ID= 3,EMPLOYEE_ID= 2, OVERALL_SCORE= 1200 },
new Rankings{ JOB_ID= 2,EMPLOYEE_ID= 1, OVERALL_SCORE= 1600 },
new Rankings{ JOB_ID= 2,EMPLOYEE_ID= 2, OVERALL_SCORE= 1800 },
new Rankings{ JOB_ID= 4,EMPLOYEE_ID= 1, OVERALL_SCORE= 2000 },
new Rankings{ JOB_ID= 4,EMPLOYEE_ID= 2, OVERALL_SCORE= 2100 },
new Rankings{ JOB_ID= 1,EMPLOYEE_ID= 1, OVERALL_SCORE= 6400 },
};
var cpy = new List<Rankings>(rankings);
var result = new List<Rankings>();
while (cpy.Count() > 0)
{
var first = cpy.First();
result.Add(first);
cpy.RemoveAll(r => r.EMPLOYEE_ID == first.EMPLOYEE_ID || r.JOB_ID == first.JOB_ID);
}
result:
+--------+-------------+---------------+
| JOB_ID | EMPLOYEE_ID | OVERALL_SCORE |
+--------+-------------+---------------+
| 4 | 3 | 800 |
| 3 | 1 | 800 |
| 2 | 2 | 1800 |
+--------+-------------+---------------+
score:0
Basically you could use System.Linq.Distinct
method reinforced with the custom equality comparer IEqualityComparer<Ranking>
. The System.Linq
provide this method out of the box.
public class Comparer : IEqualityComparer<Ranking>
{
public bool Equals(Ranking l, Ranking r)
{
return l.JOB_ID == r.JOB_ID || l.EMPLOYEE_ID == r.EMPLOYEE_ID;
}
public int GetHashCode(Ranking obj)
{
return 1;
}
}
The trick here is with the GetHashCode
method, and then as simple as this
rankings.Distinct(new Comparer())
score:1
Really, if you're trying to get the best score for the job, you don't need to select by unique JOB_ID/EMPLOYEE_ID
, you need to sort by JOB_ID/OVERALL_SCORE
, and pick out the first matching employee per JOB_ID
(that's not already in the "assigned list").
You could get the items in order using LINQ:
var sorted = new List<Ranking>
(
rankings
.OrderBy( r => r.JOB_ID )
.ThenBy( r => r.OVERALL_SCORE )
);
...and then peel off the employees you want...
var best = new List<Ranking>( );
sorted.ForEach( r1 =>
{
if ( !best.Any
(
r2 =>
r1.JOB_ID == r2.JOB_ID
||
r1.EMPLOYEE_ID == r2.EMPLOYEE_ID
) )
{
best.Add( r1 );
}
} );
Instead of using Linq to produce a sorted list, you could implement IComparable<Ranking>
on Ranking
and then just sort your rankings:
public class Ranking : IComparable<Ranking>
{
int IComparable<Ranking>.CompareTo( Ranking other )
{
var jobFirst = this.JOB_ID.CompareTo( other.JOB_ID );
return
jobFirst == 0?
this.OVERALL_SCORE.CompareTo( other.OVERALL_SCORE ):
jobFirst;
}
//--> other stuff...
}
Then, when you Sort() the Rankings, they'll be in JOB_ID/OVERALL_SCORE order. Implementing IComparable<Ranking>
is probably faster and uses less memory.
Note that you have issues...maybe an unstated objective. Is it more important to fill the most jobs...or is more important to find work for the most employees? The route I took does what you suggest, and just take the best employee for the job as you go...but, maybe, the only employee for job 2 may be the same as the best employee for job 1...and if you put him/her on job 1, you might not have anybody left for job 2. It could get complicated :-)
Source: stackoverflow.com
Related Articles
- Finding multiple unique matches from List<object> where two criteria have to be different
- Query with LINQ where Context matches multiple parameters from a List of Objects
- Filter ICollection of objects where any criteria matches the ones from a list of ints usin Linq
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- Using LINQ to create pairs from two different list where the entries have same attribute
- creating Linq to sqlite dbml from DbLinq source code
- How to check multiple attributes having unique combination within List where attribute names are available as separate collection. [C#]
- LINQ Query to Filter Items By Criteria From Multiple Lists
- Linq select from list where property matches a condition
- Get first value from table with where either have the value match or null with LINQ
- Selecting from list based on multiple unique fields
- C# SQL/Linq/Entity Framework calculating column totals for multiple columns from large data source
- Returning a collection of objects where an objects property matches any property from another collection of objects using LINQ-to-Entities
- Combining multiple dictionary where key matches
- need get unique set of keys from Multiple dictionaries<Datetime,double>
- Linq - Get all keys from dictionary where all values in the value list are equal to criteria
- Get result from multiple where conditional statements in LINQ
- Fetch (x,y) from a 2D Array where condition matches
- Using a Linq query to select objects where any value of a property matches values from a list
- LINQ. Request for understanding multiple select and from and where in C#
- LINQ XML Get value of element from multiple where statement
- Linq Get count from multiple where condition
- How to retrieve data from multiple tables that may or may not have values in common
- C# Linq XML Query where multiple elements of same name from a parent node based on a child node value
- Get All Entities From EntityCollection Where Values Matches X
- finding the highest element from an integer array which doesn't have duplicate
- c# Linq or code to extract groups from a single list of source data
- Entity Framework Linq Query: How to Where on Multiple Nav Properties and Select from 3rd Nav Property
- Query data from List<dynamic> which have Multiple type objects
- Entity to LINQ upload CSV file where single rows can have multiple values in columns
- c# returning class implementation in lambda
- c# Linq Object subQuery / In
- get value Session
- C# create DataView from DataView
- LINQ - GroupBy over many-to-many relation
- What is the best way to optimize or "tune" LINQ expressions?
- Distinct in LINQ and change result type
- Linq - Remove object in nested collections
- Passing Parameters from View to Action in MVC
- CSV Delimited to XML - Folder Hierarchy
- Windbg Linq for managed objects
- Creating a list filled with new instances of an object
- Using LINQ for List of type object
- add multiple xml elements value into a list
- Using Linq to loop through all controls only get the first control
- LINQ: Order By Anonymous Type
- Extension method not updating object passed in
- how to get and store second last order by datetime element in json data in c#
- Linq - Left Outer Join - DataTables
- How to ignore 'where' and 'order by' condition if the column is null in LINQ