Linq to group by two fields and average - c#

I have the following C# models:
public class RawData
{
public int questionnaireId { get; set; }
public int coachNodeId { get; set; }
public int questionnaireNumber { get; set; }
public float score { get; set; }
}
public class AveragedData
{
public int coachNodeId { get; set; }
public int questionnaireNumber { get; set; }
public float averageScore { get; set; }
}
I have an API endpoint which is returning data from a database, mapped as List<RawData>. The values are like this:
questionnaireId | coachNodeId | questionnaireNumber | score
1 | 30 | 1 | 2
2 | 40 | 1 | 3
3 | 30 | 2 | 1
4 | 30 | 3 | 4
5 | 40 | 2 | 5
6 | 40 | 1 | 5
7 | 30 | 1 | 1
8 | 30 | 1 | 2
9 | 40 | 1 | 2
10 | 30 | 2 | 4
What I need to do now, in a LINQ query, is to average out the score values grouped by coachNodeId and questionnaireNumber and return a list of type AveragedData.
The values returned by averaging and grouping the example data above, should be:
coachNodeId | questionnaireNumber | averageScore
30 | 1 | 1.66666666 (calculated by: (2 + 1 + 2) / 3))
30 | 2 | 2.5 (calculated by: (1 + 4) / 2))
30 | 3 | 4 (calculated by: (4 / 1))
40 | 1 | 3.33333333 (calculated by: (3 + 5 + 2) / 3))
40 | 2 | 5 (calculated by: (5 / 1))
I'm not experienced with LINQ so am struggling to put together a query that groups by both coachNodeId and questionnaireNumber and averages the score, returning an object of type List<AveragedData>. Could anyone suggest how to accomplish this?
Many thanks.

assuming you have a List<RawData> called list, you are wanting:
var results = list.GroupBy(x => new
{
questionnaire = x.questionnaireId,
coach = x.coachNodeId
})
.Select(x => new AveragedData
{
coachNodeId = x.Key.coach,
questionnaireNumber = x.Key.questionnaire,
averageScore = x.Average(xx => xx.score)
})
.ToList();
Do the grouping, then use a Select to project the data to your type, using LINQ's Average as well.

Try following :
DataTable dt = new DataTable();
dt.Columns.Add("questionnaireId", typeof(int));
dt.Columns.Add("coachNodeId", typeof(int));
dt.Columns.Add("questionnaireNumber", typeof(int));
dt.Columns .Add("score", typeof(int));
dt.Rows.Add(new object[] {1,30, 1, 2});
dt.Rows.Add(new object[] {2,40, 1, 3});
dt.Rows.Add(new object[] {3,30, 2, 1});
dt.Rows.Add(new object[] {4,30, 3, 4});
dt.Rows.Add(new object[] {5,40, 2, 5});
dt.Rows.Add(new object[] {6,40, 1, 5});
dt.Rows.Add(new object[] {7,30, 1, 1});
dt.Rows.Add(new object[] {8,30, 1, 2});
dt.Rows.Add(new object[] {9,40, 1, 2});
dt.Rows.Add(new object[] {10,30, 2, 4});
var averages = dt.AsEnumerable()
.GroupBy(x => new { coachNodeId = x.Field<int>("coachNodeId"), questionnaireNumber = x.Field<int>("questionnaireNumber") })
.Select(x => new { coachNodeId = x.Key.coachNodeId, questionnaireNumber = x.Key.questionnaireNumber, average = x.Average(y => y.Field<int>("score")) })
.ToList();

Related

C# logic to find offset and fetch values

I get the below resultset from a SQL query and I store it in var.
+-----------+--------+
| Rownumber | Data |
+-----------+--------+
| 0 | 9 |
| 1 | 0 |
| 2 | 4 |
| 3 | 9 |
| 4 | 15 |
| 5 | 2 |
| 6 | 1 |
| 7 | 6 |
| 8 | 0 |
| 9 | 4 |
| 10 | 1 |
| 11 | 1 |
| 12 | 1 |
| 13 | 1 |
| 14 | 1 |
| 15 | 1 |
| 16 | 1 |
| 17 | 1 |
| 18 | 1 |
| 19 | 1 |
| 20 | 1 |
| 21 | 1 |
| 22 | 1 |
+-----------+--------+
I want to write a logic in c# :
I want to add the Data column sequentially.
If the summed Data column value is more than or equal to 15, then I want to store the following value in two variables:
offset = The starting point of rownumber
Fetch = Num of rows taken to achieve the sum 15
E.g:
Iteration 1:
+-----------+--------+
| Rownumber | Data |
+-----------+--------+
| 0 | 9 |
| 1 | 0 |
| 2 | 4 |
| 3 | 9 |
+-----------+--------+
Expected variable values:
offset = 0
Fetch = 4 (num of rows taken to achieve the value of 15. Sum of value should be >= 15)
Iteration 2 :
+-----------+--------+
| Rownumber | Data |
+-----------+--------+
| 4 | 15 |
+-----------+--------+
Expected values:
offset = 4
Fetch = 1 (num of rows taken to achieve the value of 15)
Iteration 3:
+-----------+--------+
| Rownumber | Data |
+-----------+--------+
| 5 | 2 |
| 6 | 1 |
| 7 | 6 |
| 8 | 0 |
| 9 | 4 |
| 10 | 1 |
| 11 | 1 |
+-----------+--------+
Expected values:
offset = 5
Fetch = 7 (num of rows taken to achieve the value of 15)
The iteration will go on until the last value.
I supposed your model look like the following
public class Data
{
public int Rownumber { get; set; }
public int data { get; set; }
}
public class Result
{
public int offset { get; set; }
public int fetsh { get; set; }
}
and you need the following code
public List<Result> GetResults(List<Data> data)
{
var sum = 0;
var start_taking_index = 0;
List<Result> results = new List<Result>();
for (int i = 0; i < data.Count; i++)
{
sum += data[i].data;
if(sum >= 15 || i == data.Count-1)
{
// if the sum exceed 15 create new result
results.Add(new Result
{
offset = start_taking_index,
fetsh = i - start_taking_index +1,
});
// then reset the tracking variables
start_taking_index = i+1;
sum = 0;
}
}
return results;
}
here is xUnit test the scenario in the question
[Fact]
public void GetResults_test()
{
List<Data> datas = new List<Data>()
{
new Data{Rownumber = 0,data= 9},
new Data{Rownumber = 1,data= 0},
new Data{Rownumber = 2,data= 4},
new Data{Rownumber = 3,data= 9},
new Data{Rownumber = 4,data=15},
new Data{Rownumber = 5,data= 2},
new Data{Rownumber = 6,data= 1},
new Data{Rownumber = 7,data= 6},
new Data{Rownumber = 8,data= 0},
new Data{Rownumber = 9,data= 4},
new Data{Rownumber = 10,data= 1},
new Data{Rownumber = 11,data= 1},
new Data{Rownumber = 12,data= 1},
new Data{Rownumber = 13,data= 1},
new Data{Rownumber = 14,data= 1},
new Data{Rownumber = 15,data= 1},
new Data{Rownumber = 16,data= 1},
new Data{Rownumber = 17,data= 1},
new Data{Rownumber = 18,data= 1},
new Data{Rownumber = 19,data= 1},
new Data{Rownumber = 20,data= 1},
new Data{Rownumber = 21,data= 1},
new Data{Rownumber = 22,data= 1},
};
var result = GetResults(datas);
Assert.NotEmpty(result);
// first
Assert.Equal(0,result[0].offset);
Assert.Equal(4,result[0].fetsh);
// second
Assert.Equal(4, result[1].offset);
Assert.Equal(1, result[1].fetsh);
//
Assert.Equal(5, result[2].offset);
Assert.Equal(7, result[2].fetsh);
//
Assert.Equal(12, result[3].offset);
Assert.Equal(11, result[3].fetsh);
// total count count
Assert.Equal(4, result.Count);
}
I would go with:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<Data> datas = new List<Data>()
{
new Data{Rownumber = 0,data= 9},
new Data{Rownumber = 1,data= 0},
new Data{Rownumber = 2,data= 4},
new Data{Rownumber = 3,data= 9},
new Data{Rownumber = 4,data=15},
new Data{Rownumber = 5,data= 2},
new Data{Rownumber = 6,data= 1},
new Data{Rownumber = 7,data= 6},
new Data{Rownumber = 8,data= 0},
new Data{Rownumber = 9,data= 4},
new Data{Rownumber = 10,data= 1},
new Data{Rownumber = 11,data= 1},
new Data{Rownumber = 12,data= 1},
new Data{Rownumber = 13,data= 1},
new Data{Rownumber = 14,data= 1},
new Data{Rownumber = 15,data= 1},
new Data{Rownumber = 16,data= 1},
new Data{Rownumber = 17,data= 1},
new Data{Rownumber = 18,data= 1},
new Data{Rownumber = 19,data= 1},
new Data{Rownumber = 20,data= 1},
new Data{Rownumber = 21,data= 1},
new Data{Rownumber = 22,data= 1},
};
foreach(var entry in Calculate(datas))
{
Console.WriteLine("Offset: " + entry.Key + " | Fetch: " + entry.Value);
}
}
public static List<KeyValuePair<int, int>> Calculate(List<Data> data)
{
var result = new List<KeyValuePair<int, int>>();
int offset = 0, lastOffset = 0;
int sum = 0;
foreach(var entry in data)
{
sum += entry.data;
if(sum >= 15)
{
result.Add(new KeyValuePair<int, int>(lastOffset, offset - lastOffset + 1));
sum = 0;
lastOffset = offset + 1;
}
offset++;
}
return result;
}
public class Data
{
public int Rownumber { get; set; }
public int data { get; set; }
}
}
Mind that it does not have any guards - it's up to you.
System.Linq may do trick for you with Skip and TakeWhile extension methods:
static void Main()
{
// Fill source info
var source = new List<(int, int)>();
for (int i = 0; i <= 22; i++)
source.Add((i, i switch
{
0 => 9,
1 or 8 => 0,
2 or 9 => 4,
3 => 9,
4 => 15,
5 => 2,
7 => 6,
_ => 1,
}));
// Fetching result
foreach (var (offset, fetch) in GetResult(source))
Console.WriteLine($"Offset: {offset} | Fetch: {fetch}");
// Output:
// Offset: 0 | Fetch: 4
// Offset: 4 | Fetch: 1
// Offset: 5 | Fetch: 7
// Offset: 12 | Fetch: 11
Console.ReadKey();
}
static List<(int, int)> GetResult(List<(int, int)> source)
{
var result = new List<(int, int)>();
var proceededRecords = 0;
while (proceededRecords < source.Count)
{
var offset = proceededRecords;
var dataSum = 0;
var fetch = source.Skip(proceededRecords).TakeWhile((data, _) =>
{
if (dataSum >= 15)
return false;
dataSum += data.Item2;
return true;
}).Count();
proceededRecords += fetch;
result.Add((offset, fetch));
}
return result;
}
Remarks.
I used tuples to simplify example and avoid creating some Model class with RowNumber and Data properties or Result class with Offset and Fetch properties.
The idea was to loop over source collection with taking some unknown amount of tuples until sum of 2nd value in tuple is less than 15. TakeWhile help me with that. Skip was used to... skip amount of already fetched records.
While your example shows that your data indexes are sequential and start at zero, it was not stated explicitely that it will be always the case.
This solution works even if the index of the data from your database is not starting from 0, or is not sequencial (or both). Or if instead of an index you had any other kind of identifier, such as a timestamp for instance.
i.e. if your data is like
+-----------+--------+
| Rownumber | Data |
+-----------+--------
| 6 | 1 |
| 7 | 6 |
| 8 | 0 |
| 9 | 4 |
etc...
+-----------+--------+
or
+-----------+--------+
| Rownumber | Data |
+-----------+--------
| 16 | 1 |
| 7 | 6 |
| 108 | 0 |
| 9 | 4 |
| 1910 | 1 |
| 121 | 1 |
etc..
+-----------+--------+
or even
+-----------------------+--------+
| Timestamp | Data |
+-----------------------+--------+
| 2021/03/02 - 10:06:24 | 1 |
| 2021/03/02 - 12:13:03 | 6 |
| 2021/03/04 - 02:48:57 | 0 |
| 2021/05/23 - 23:38:17 | 4 |
etc...
+-----------------------+--------+
Here is the part of the code actually doing the work. It does not really add complexity compared to a solution that would work only on zero starting sequencial indexes:
var Results = new List<Result>();
var Group = new List<Data>();
var Sum = 0;
var CurrentIndex = 0;
while (Source.Any())
{
CurrentIndex = 0;
Sum = 0;
while (Sum < 15 && CurrentIndex < Source.Count)
{
Sum += Source[CurrentIndex].Value;
CurrentIndex++;
}
Group = Source.Take(CurrentIndex).ToList();
Source = Source.Skip(CurrentIndex).ToList();
Results.Add(new Result
{
Root = Group.First().Index,
Fetch = Group.Count
});
}
What it does is rather simple:
It enumerates the first elements of your collection (source) while their sum is inferior to 15 (and there is still some elements to enumerate).
It counts the number of elements just enumerated (the fetch) and get the index of the first element (the root).
It then constructs a new collection by removing the elements that were just enumerated, and starts again, using that new collection until there is no more elements to enumerate.
That is all.
The Group variable could be avoided alltogether. It would give the following code. I prefered keeping it in my example as it shows that the group itself could be used to perform any kind of operation on its content if needed.
while (Source.Any())
{
CurrentIndex = 0;
Sum = 0;
while (Sum < 15 && CurrentIndex < Source.Count)
{
Sum += Source[CurrentIndex].Value;
CurrentIndex++;
}
Results.Add(new Result
{
Root = Source.First().Index,
Fetch = CurrentIndex
});
Source = Source.Skip(CurrentIndex).ToList();
}
By the way, the second nested while loop could be avoided by using Linq, see below. However this Linq query is particular and should be used with caution.
The TakeWhile method is using an unpure lambda, i.e. the lambda relies on external data: the captured variable Sum.
While this works perfectly fine, be aware that generally this kind of Linq query could lead to problems further down the road. For instance adding .AsParallel() to such kind of query would not work at all.
while (Source.Any())
{
Sum = 0;
Group = Source.TakeWhile(e =>
{
if (Sum < 15)
{
Sum += e.Value;
return true;
}
return false;
}).ToList();
Results.Add(new Result
{
Root = Group.First().Index,
Fetch = Group.Count
});
Source = Source.Skip(Group.Count).ToList();
}
Here is the complete code, as a full runnable Linqpad query, with randomly generated data:
void Main()
{
// Random data set preparation.
var Rnd = new Random();
var NonSequencialIndexes = Enumerable
.Range(100, 300)
.Where(i => Rnd.Next(2) == 1)
.OrderBy(i => Guid.NewGuid())
.Take(30)
.ToArray();
var Source = Enumerable
.Range(0, 30)
.Select(i => new Data
{
Index = NonSequencialIndexes[i],
Value = Rnd.Next(16)
})
.ToList()
.Dump("Random data set");
// Actual code
var Results = new List<Result>();
var Group = new List<Data>();
var Sum = 0;
var CurrentIndex = 0;
while (Source.Any())
{
CurrentIndex = 0;
Sum = 0;
while (Sum < 15 && CurrentIndex < Source.Count)
{
Sum += Source[CurrentIndex].Value;
CurrentIndex++;
}
Group = Source.Take(CurrentIndex).ToList();
Source = Source.Skip(CurrentIndex).ToList();
Results.Add(new Result
{
Root = Group.First().Index,
Fetch = Group.Count
});
}
// Display results
Results.Dump("Results");
}
// You can define other methods, fields, classes and namespaces here
public class Data
{
public int Index { get; set; }
public int Value { get; set; }
}
public class Result
{
public int Root { get; set; }
public int Fetch { get; set; }
}
An example of result:
+------+-------+
| Root | Fetch |
+------+-------+
| 346 | 3 |
+------+-------+
| 121 | 3 |
+------+-------+
| 381 | 2 |
+------+-------+
| 110 | 2 |
+------+-------+
| 334 | 2 |
+------+-------+
| 226 | 2 |
+------+-------+
| 148 | 2 |
+------+-------+
| 114 | 3 |
+------+-------+
| 397 | 3 |
+------+-------+
| 274 | 3 |
+------+-------+
| 135 | 3 |
+------+-------+
| 386 | 2 |
+------+-------+
for this data collection
+-------+------+
| Index | Value|
+-------+------+
| 346 | 0|
+-------+------+
| 294 | 14|
+-------+------+
| 152 | 11|
+-------+------+
| 121 | 3|
+-------+------+
| 234 | 6|
+-------+------+
| 393 | 13|
+-------+------+
| 381 | 8|
+-------+------+
| 305 | 15|
+-------+------+
| 110 | 13|
+-------+------+
| 357 | 9|
+-------+------+
| 334 | 8|
+-------+------+
| 214 | 13|
+-------+------+
| 226 | 6|
+-------+------+
| 248 | 15|
+-------+------+
| 148 | 12|
+-------+------+
| 131 | 9|
+-------+------+
| 114 | 3|
+-------+------+
| 250 | 4|
+-------+------+
| 217 | 11|
+-------+------+
| 397 | 3|
+-------+------+
| 312 | 7|
+-------+------+
| 191 | 7|
+-------+------+
| 274 | 7|
+-------+------+
| 292 | 6|
+-------+------+
| 277 | 14|
+-------+------+
| 135 | 2|
+-------+------+
| 240 | 12|
+-------+------+
| 163 | 12|
+-------+------+
| 386 | 12|
+-------+------+
| 330 | 5|
+-------+------+

C# LINQ Select table from list with multiple fields

I'd like to select many values from a table with a list.
I have FabricTable(Year is int):
+----+-------+---------+------+
| Id | Color | Texture | Year |
+----+-------+---------+------+
| 1 | Red | Rough | 2019 |
+----+-------+---------+------+
| 2 | Green | Soft | 2019 |
+----+-------+---------+------+
| 3 | Blue | Rough | 2019 |
+----+-------+---------+------+
| 4 | Red | Med | 2019 |
+----+-------+---------+------+
| 5 | Blue | Soft | 2018 |
+----+-------+---------+------+
I have selectedItems list (year is int):
+---------+------+
| Texture | Year |
+---------+------+
| Rough | 2019 |
+---------+------+
| Soft | 2019 |
+---------+------+
I'd like to get the Id from table, it should result with Id = 1, 2, & 3.
How can I achieve this with Linq in C#? I just need to select by Texture & Year
Here's what I've tried but I'm not sure how to select from list with multiple values(selectedItems is a list but I don't know how to query multiple columns):
db.FabricTable.Select(o => o.Texture == selectedItems.Texture && o.Year == selectItems.Year)
You get a compiler error when using selectedItems.Texture because selectedItem is a list that contains an object with the Texture property. You need to check all of the items in the list when searching for the desired items in FabricTable:
var items = db.FabricTable.Where(o => selectedItems.Any(selectedItem => o.Texture == selectedItem.Texture && o.Year == selectedItem.Year));
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication120
{
class Program
{
static void Main(string[] args)
{
List<Item> items = new List<Item>() {
new Item() { Id = 1, Color = "Red", Texture = "Rough", Year = 2019},
new Item() { Id = 2, Color = "Green", Texture = "Soft", Year = 2019},
new Item() { Id = 3, Color = "Blue", Texture = "Rough", Year = 2019},
new Item() { Id = 4, Color = "Red", Texture = "Soft", Year = 2018}
};
DataTable dt = new DataTable();
dt.Columns.Add("Color", typeof(string));
dt.Columns.Add("Texture", typeof(string));
dt.Columns.Add("Year", typeof(int));
foreach (Item item in items.Where(x => (x.Texture == "Rough") && (x.Year == 2019)))
{
dt.Rows.Add(new object[] { item.Color, item.Texture, item.Year });
}
}
}
public class Item
{
public int Id { get; set; }
public string Color { get; set; }
public string Texture { get; set; }
public int Year { get; set; }
}
}

Finding multiple unique matches from List<object> where two criteria have to be different

I am having trouble selecting the first item in a list that is unique based on two fields, JOB_ID and EMPLOYEE_ID.
Each job should only be assigned to one employee (the one with the lowest OVERALL_SCORE), then move on and assign the next employee.
The List Objects are as follows:
JobMatch.cs
public int JOB_ID { get; set; }
public int JOB_MATCHES_COUNT { get; set; }
EmployeeMatch.cs
public int EMPLOYEE_ID { get; set; }
public int EMPLOYEE_MATCHES_COUNT { get; set; }
Rankings.cs
public int JOB_ID { get; set; }
public int EMPLOYEE_ID { get; set; }
public int TRAVEL_TIME_MINUTES { get; set; }
public int PRIORITY { get; set; }
public int OVERALL_SCORE { get; set; }
Rankings.cs gets an overall score based on the travel time field and
number of matches an Employee/Job has.
EmployeeMatch.cs
+-------------+-------------------+
| EMPLOYEE_ID | EMP_MATCHES_COUNT |
+-------------+-------------------+
| 3 | 1 |
| 4 | 1 |
| 2 | 3 |
| 1 | 4 |
+-------------+-------------------+
JobMatch.cs
+--------+-------------------+
| JOB_ID | JOB_MATCHES_COUNT |
+--------+-------------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 4 |
+--------+-------------------+
Ranking.cs (shortened as to not fill the screen)
+--------+-------------+---------------+
| JOB_ID | EMPLOYEE_ID | OVERALL_SCORE |
+--------+-------------+---------------+
| 4 | 3 | 800 |
| 4 | 4 | 800 |
| 3 | 1 | 800 |
| 3 | 2 | 1200 |
| 2 | 1 | 1600 |
| 2 | 2 | 1800 |
| 4 | 1 | 2000 |
| 4 | 2 | 2100 |
| 1 | 1 | 6400 |
+--------+-------------+---------------+
Basically, the idea is to select the first unique Employee and Job in this list and then the best matches will be put into a separate list, something like the following for the above scenario:
+--------+-------------+---------------+
| JOB_ID | EMPLOYEE_ID | OVERALL_SCORE |
+--------+-------------+---------------+
| 4 | 3 | 800 |
| 3 | 1 | 800 |
| 2 | 2 | 1800 |
+--------+-------------+---------------+
I tried the following but it didn't work as intended:
var FirstOrder = (rankings.GroupBy(u => u.JOB_ID)
.Select(g => g.First())).ToList();
var SecondOrder = (FirstOrder.GroupBy(u => u.EMPLOYEE_ID)
.Select(g => g.First())).ToList();
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 |
+--------+-------------+---------------+
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 :-)
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())

Count and Max Columns Group By in LINQ

I have model
public class Rate
{
public int Nr{ get; set; }
public string Rate{ get; set; }
public int Order{ get; set; }
}
and a RateList = List<Rate> like this
Nr | Rate | Order
123 | A | 2
425 | A+ | 1
454 | B | 4
656 | B+ | 3
465 | A | 2
765 | B | 4
Notice that Order always match the Rate (A+ = 1, A = 2, B+ = 3, B = 4, C+ = 5 ...)
I want to count how many time the Rate occoured and display order by the Order
The result should look like this
Rate | Count | Order
A+ | 1 | 1
A | 2 | 2
B+ | 1 | 3
B | 2 | 4
or without column Order
Rate | Count
A+ | 1
A | 2
B+ | 1
B | 2
In SQL I could do like this if I had above list in table Tab
SELECT Rate, COUNT(Rate), Max(Order) from Tab group by Rate
but in LINQ?
I was trying something like this
var rating= RateList.Distinct().GroupBy(x => x.Rate)
.Select(x => new { Rate = x.Key, RateCount = x.Count() })
.OrderBy(x => x.Order);
but didnt work.
Thank You for help.
Your SQL query is equevalent to:
var rating = rateList.GroupBy(x => x.Rate)
.Select(x => new {
Rate = x.Key,
RateCount = x.Count(e => e != null),
Max = x.Max(g => g.Order)
});

linq joining, grouping, with parent roll-up

say I've got a DataTable in this format:
id | key1 | key2 | data1 | data2 | parentID
10 | AA | one | 10.3 | 0.3 | -1
10 | AA | two | 20.1 | 16.2 | -1
10 | BB | one | -5.9 | 30.1 | -1
20 | AA | one | 403.1 | -20.4 | 10
30 | AA | one | 121.5 | 210.3 | -1
and a second DataTable like so:
id | data
10 | 5500
20 | -3000
30 | 500
what I want to do is aggregate the data at the "id" level, with the second table's "data" field added to the first's net "data1", and "data2" just summed up by itself. I figured out how to do this, but what I'm stuck at is this: I want data for anything with "parentID" != -1 to be added to it's parent. so the output of the above data should be
id | data1 | data2
10 | 2927.6 | 26.2
30 | 621.5 | 210.3
is there an efficient way to do this?
edit: code sample
DataTable dt1 = new DataTable();
dt1.Columns.Add("id", typeof(int));
dt1.Columns.Add("key1", typeof(string));
dt1.Columns.Add("key2", typeof(string));
dt1.Columns.Add("data1", typeof(double));
dt1.Columns.Add("data2", typeof(double));
dt1.Columns.Add("parentID", typeof(int));
DataTable dt2 = new DataTable();
dt2.Columns.Add("id", typeof(int));
dt2.Columns.Add("data", typeof(double));
dt1.Rows.Add(new object[] { 10, "AA", "one", 10.3, 0.3, -1 });
dt1.Rows.Add(new object[] { 10, "AA", "two", 20.1, 16.2, -1 });
dt1.Rows.Add(new object[] { 10, "BB", "one", -5.9, 30.1, -1 });
dt1.Rows.Add(new object[] { 20, "AA", "one", 403.1, -20.4, 10 });
dt1.Rows.Add(new object[] { 30, "AA", "one", 121.5, 210.3, -1 });
dt2.Rows.Add(new object[] { 10, 5500 });
dt2.Rows.Add(new object[] { 20, -3000 });
dt2.Rows.Add(new object[] { 30, 500 });
var groups = dt1.AsEnumerable()
.GroupBy(e => e["id"])
.Select(e => new
{
id = e.Key,
net_data1 = e.Sum(w => (double)w["data1"]),
net_data2 = e.Sum(w => (double)w["data2"])
})
.GroupJoin(dt2.AsEnumerable(), e1 => e1.id, e2 => e2["id"],
(a1, a2) => new
{
id = a1.id,
net_data1 = a1.net_data1 + a2.Sum(w => (double)w["data"]),
net_data2 = a1.net_data2
});
Unfortunately, SQL (and, by extension, LINQ) is not well-suited to recursion. Can the parentID column go multiple levels deep? Like this:
ID Parent
------------------
10 -1
20 10
30 10
40 20
If you want to retrace the steps up from ID 40 to ID 10, then you should abandon a SQL/LINQ approach and just do it in code.
It sounds like a good use of a group join. Something like this might work (though it's completely untested):
var items = from parent in context.dataTable
join child in context.dataTable on parent.id equals child.parentID into children
where parent.parentID == -1
select new { id = parent.id,
data1 = (parent.data1 + children.Sum(c => c.data1)),
data2 = (parent.data2 + children.Sum(c => c.data2)) };

Categories

Resources