Solved

Find 10 top values between two generic lists

Posted on 2015-01-08
1
76 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now