Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:24:42+00:00 2026-06-14T17:24:42+00:00

I have the following type: public class Parts { public string PartNo { get;

  • 0

I have the following type:

public class Parts
{
    public string PartNo { get; set; }
    public decimal Price { get; set; }
}

what I would like to do is to compare the price or each part with the cheapest one and display the difference in percentage.

This is what I have tried so far and it works:

        var part1 = new Part {PartNo = "part1", Price = 10};
        var part2 = new Part {PartNo = "part1", Price = 8};
        var part3 = new Part {PartNo = "part1", Price = 12};

        var parts = new List<Part> {part1, part2, part3};

        var list = from p in parts
                    orderby p.Price ascending 
                   select p;

        var sb = new StringBuilder();

        var counter = 0;
        decimal firstPrice=0;
        foreach (Part p in list)
        {
            if (counter == 0)
            {
                firstPrice = p.Price;
            }

            sb.Append(p.PartNo + ": " + p.Price + "," + ((p.Price/firstPrice)-1)*100 + Environment.NewLine);

            counter++;
        }


        Console.WriteLine(sb.ToString(), "Parts List");

This outputs the following:

part1: 8, 0
part1: 10, 25.00
part1: 12, 50.0

This shows the price increase for each each part, and that is what I am trying to achieve but I was wondering is there a better way of calculating the price difference in percentage (e.g. with a LINQ query) or in any other way.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:24:43+00:00Added an answer on June 14, 2026 at 5:24 pm

    I would calculate the difference as first step.

    var cheapestPrice = parts.Min(p => p.Price);
    var list = parts.Select(p => new { 
        Part = p,
        DiffPercentage = ((p.Price - cheapestPrice) / cheapestPrice) * 100 
    });
    
    foreach (var p in list)
        Console.WriteLine("{0}: {1},{2}%", p.Part.PartNo, p.Part.Price, p.DiffPercentage);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following test.pyx cdef public class Foo[object Foo, type FooType]: cdef public char*
I have following webservice: [webmethod] public string MakeReservation(?? PassengersInfo)//what data type use for PassengerInfo
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have defined the following Model public class TodayOrder { public string ID_ORIG {
I have the following classes class A{ private String name; private int value; public
Given the class: public class Foo { public string Name { get; set; }
I have the following code: Type type = typeof(T); foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public
I have a DbContext with several of the following type of members: public DbSet<JobLevel>
When using ASP.net webforms my usual solution would have following type of setup -
I have the following model (dumbed down for this question): public class Student {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.