none
Linq query To Calculate values inside my object RRS feed

  • Question

  • Hi , 

    I have below data inside my object 

    var summary =  new List<OrderSummary>();

    summary.Add(new OrderSummary(){Name = "John", Expected=10, Actual = 10});

    summary.Add(new OrderSummary(){Name = "John", Expected=20, Actual = 40});

    now i wanted to create a new object (CalculationSummary)  based on the above object values with below properties  on it with below values 

    var  name = "John";

    var totalExpected = 30;

    var actualExpected = 50 ; 

    var performance = 1.66  // calculation (50/30)

    can we write a linqquery to get this figures in a single line ? 

    thanks 



    • Edited by kavithma Wednesday, May 20, 2020 3:05 PM
    Wednesday, May 20, 2020 2:52 PM

Answers

  • If you need it just for John, not for all people, then check something like this:

    CalculationSummary john_data = summary
    	.Where( s => s.Name == "John" )
    	.Aggregate( new CalculationSummary { Name = "John" }, 
    		( a, n ) => { a.TotalExpected += n.Expected; a.ActualExpected += n.Actual; return a; } );
    


    • Edited by Viorel_MVP Wednesday, May 20, 2020 3:35 PM
    • Marked as answer by kavithma Wednesday, May 20, 2020 3:40 PM
    • Unmarked as answer by kavithma Wednesday, May 20, 2020 4:04 PM
    • Marked as answer by kavithma Wednesday, May 20, 2020 4:11 PM
    Wednesday, May 20, 2020 3:35 PM

All replies

  • Try the following.

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApp1
    {
        public class Demo
        {
    
            public static void Main(string[] args)
            {
                var summary = new List<OrderSummary>()
                {
                    new OrderSummary()
                    {
                        Name = "John",
                        Actual = 50,
                        Expected =  30
                    }
                };
                var first = summary.FirstOrDefault();
                Console.WriteLine($"{first.Name} {first.Expected} {first.Actual} {first.Performance}");
                Console.ReadLine();
            }
    
        }
    
        class OrderSummary
        {
            public string Name { get; set; }
            public double Expected { get; set; }
            public double Actual { get; set; }
            public double Performance => Actual / Expected;
        }
    } 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by kavithma Wednesday, May 20, 2020 3:40 PM
    • Unmarked as answer by kavithma Wednesday, May 20, 2020 4:03 PM
    Wednesday, May 20, 2020 3:17 PM
    Moderator
  • Hi thanks for the answer, but how do we get the name , actual and expected values from the  list of summary object and assign it to OrderSummary property ? 
    Wednesday, May 20, 2020 3:23 PM
  • because  Expected = 30 comes from the total  value of   (10 + 20) 
    Wednesday, May 20, 2020 3:24 PM
  • If you need it just for John, not for all people, then check something like this:

    CalculationSummary john_data = summary
    	.Where( s => s.Name == "John" )
    	.Aggregate( new CalculationSummary { Name = "John" }, 
    		( a, n ) => { a.TotalExpected += n.Expected; a.ActualExpected += n.Actual; return a; } );
    


    • Edited by Viorel_MVP Wednesday, May 20, 2020 3:35 PM
    • Marked as answer by kavithma Wednesday, May 20, 2020 3:40 PM
    • Unmarked as answer by kavithma Wednesday, May 20, 2020 4:04 PM
    • Marked as answer by kavithma Wednesday, May 20, 2020 4:11 PM
    Wednesday, May 20, 2020 3:35 PM
  • Hi thanks for the answer, but how do we get the name , actual and expected values from the  list of summary object and assign it to OrderSummary property ? 
    To select those properties to another OrderSummary (which is a class instance not a property) you need to select another OrderSummary and assign the properties which was not part of this question. I've given you a solution for what was asked, for additional questions start another thread after closing this thread out.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 20, 2020 3:35 PM
    Moderator
  • Hi Thanks for the reply i have marked the post as answered, 

    just another question in here ,  I can see the 'John' has been hard coded within the linq query , but how do we get that name assign to CalculateSummary.Name  without hard coding the name ? 

    thanks 

    Wednesday, May 20, 2020 3:43 PM
  • Imaging we have more than one person's information defned on OrderSummary  list and we wanted to calculate  the summary for each person's name (group by person name) and get teir expected and actual total  ? 
    Wednesday, May 20, 2020 3:51 PM
  • If you need it for all people, not just for John, then check something like this:

    List<CalculationSummary> results = summary
    	.GroupBy( s => s.Name )
    	.Select( g => g.Aggregate( new CalculationSummary { Name = g.Key },
    			( a, n ) => { a.TotalExpected += n.Expected; a.ActualExpected += n.Actual; return a; } ) )
    	.ToList( );
    

    Wednesday, May 20, 2020 4:24 PM