Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find 10 top values between two generic lists

Posted on 2015-01-08
1
Medium Priority
?
101 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

704 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