Solved

Find 10 top values between two generic lists

Posted on 2015-01-08
1
87 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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