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 Query
- Finding multiple unique matches from List<object> where two criteria have to be different
- Using LINQ to create pairs from two different list where the entries have same attribute
- 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
- Select items from two custom lists based on multiple join criteria
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- C# / .NET comparing two large lists and finding missing items from both list
- How to select items from two lists where the ids don't match
- How to get unique values from two list of dictionaries using Linq?
- Finding common columns from two datatable and using those for Join condition in LINQ
- 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#]
- Select unique IDs from two DataTables with three queries and merge
- LINQ Query to Filter Items By Criteria From Multiple Lists
- Linq select from list where property matches a condition
- Using C# and Linq to create an array containing unique items from two input arrays
- Linq Query Filter from two lists where row differs
- C# Remove duplicates from two lists with multiple properties
- Need a LINQ code example to link two tables that have no foreign key
- 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
- Retrieving multiple objects from a list based on two properties
- Combining multiple dictionary where key matches
- need get unique set of keys from Multiple dictionaries<Datetime,double>
- Outer Joining two Data Tables on multiple criteria
- EF Core get records where two properties match a pair of properties from a list
- Join two tables with one to many relationship and pick latest from multiple side
- GroupJoin by date where date in second list between two dates from first
More Query from same tag
- MVC: Repopulate filters form's fields without using Model
- How to write a LINQ statement with a GroupBy condition
- LINQ to XML query attributes
- Help converting LINQ expression from C# to VB
- How to find duplicate record using Linq from DataTable
- Linq : Combine a list with generic type using left join into a generic type
- select count on list using linq on specific value vb.net
- How to check if an expression is empty (void)?
- Linq to sql select new string
- C# Lambda expressions with Classes
- Return navigation property within join query
- How to change synthesized speech voice language UWP?
- Dynamic LINQ API with .NET Framework 4
- How to get values out of IGrouping?
- How to use LINQ remove a subset of list with certain criteria?
- Exec Procedure who receive data from Entity Framework
- How to edit element of List of Lists?
- Nhibernate throws Object of type 'System.Linq.EnumerableQuery`1[Entity]' cannot be converted to type 'System.Linq.IQueryable`1[System.Object[]]'
- Passing statement lambda as parameter
- LINQ to XML query returning a list of all child elements
- GroupJoin and Include
- How to bind data to DTO attribute based on a condition?
- FormatException was unhandled (int.Parse)
- Show LINQ joined table column at view DisplayNameFor
- Adding if statement between Linq
- Filtering List Objects Based on Property String
- How can I read an XML file properly into a collection when the XML has a specific root element name?
- LINQ join to collection using index
- What Sorting Algorithm Is Used By LINQ "OrderBy"?
- Building a Sensor Monitoring System using RX