most answers that I have seen here are mostly covering the issue of boolean true/false upon list comparison. What I am interested in is comparing two lists and seeing the change in the value between the two lists.
In other words, I have list and list; they both have two entries-- name and a grade: in list a, the first entry is "Tom", and his grade is "100", in list b, the first entry is also "Tom", but his grade is now "89".
My pathetic attempt to do this:
private static void DataFromResults(List<data> aList, List<data> bList)
{
using (var reader = newStreamReader(File.OpenRead(@"c:\temp\data.csv")))
{
while(!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string name = new name(values[0]).ToString();
string grade = new grade(values[1]).ToString();
foreach (var data in aList)
{
for (int i = 0; i < aList.Count; i++)
{
if (aList[i] != bList[i]) {//???}...
It sounds like you just want something like:
var differences = aList.Zip(bList,
(a, b) => new { a.Name, Difference = a.Grade - b.Grade });
foreach (var result in differences)
{
if (result.Difference != 0)
{
Console.WriteLine("Change for {0}: {1}", result.Name, result.Difference);
}
}
(If you want it proportional rather than absolute, you probably want to keep the initial value as well...)
The Zip
method basically matches up elements in two sequences (in order) and projects each pair into another value, according to the projection you provide.
See more on this question at Stackoverflow