Solved

Find 10 top values between two generic lists

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

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Service with UDP 2 28
Duplicate a row 2 33
Handle null when using linq in this line 1 20
XAML: Layout 8 0
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

20 Experts available now in Live!

Get 1:1 Help Now