Solved

Find 10 top values between two generic lists

Posted on 2015-01-08
1
83 Views
Last Modified: 2016-02-17
I have two generic lists, with identical object T.
Code is like the primary key in the list.
List<T> listT1
List<T> listT2

T has the following fields
Code
Price
RetailPrice
SuggestedPrice


I need to find the top 10 codes which have the highest absolute Price difference between the two lists

Example
Code      list1   list2       difference
ABC        1000   2000   1000
LMN        500    50       450
0
Comment
Question by:countrymeister
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40538814
Here's one approach:

List<T> listT1 = new List<T>()
{
    new T() { Code = "ABC", Price = 1000 },
    new T() { Code = "LMN", Price = 500 },
};

List<T> listT2 = new List<T>()
{
    new T() { Code = "ABC", Price = 2000 },
    new T() { Code = "LMN", Price = 50 },
};

var query = from item1 in listT1
            join item2 in listT2 on item1.Code equals item2.Code
            select new
            {
                Code = item1.Code,
                list1 = item1.Price,
                list2 = item2.Price,
                difference = Math.Abs(item1.Price - item2.Price),
            };
            
var result = query.OrderByDescending(item => item.difference)
                  .Take(10);

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question