.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

SUM, Distinct, Greater than Operations on Multiple Columns data in Generic List using LINQ

Posted By:Syed Shakeer Hussain       Posted Date: December 17, 2011    Points: 40    Category: LINQ    URL: http://www.dotnetspark.com  

In this article you can learn how to Sum,Distinct,Greater than operations using LINQ
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace LinQSamples
{
    class Program
    {
        static void Main(string[] args)

          {

            SumEx1();

         }

        class Package
        {
            public string Company { get; set; }
            public double Weight { get; set; }
        }
 
       public static void SumEx1()
       {
           List packages =new List
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 },
              new Package { Company = "Adventure Works", Weight = 33.8 },
              new Package { Company = "Adventure Works", Weight = 33.8 }
            };
 
          double totalWeight = packages.Sum(pkg => pkg.Weight);
          Console.WriteLine("\n Total Weight={0}"+totalWeight);

 //Greater Than
         IEnumerable wgt = from w in packages
     where w.Weight > 10
     select  w.Weight;
                     
            Console.WriteLine("\n Greater than 10 weight is");
            foreach (double w in wgt)
            {
                Console.WriteLine("{0}", w);
            }
 

      //SUM

           double dTotalWeight = packages.Sum(s =>s.Weight);                  
           Console.WriteLine("\nTotal salary={0}", dTotalWeight );



     // Distinct Records

IEnumerable distinct =
(from c in packages select  c.Company).Distinct().ToList();
            Console.WriteLine("\n Distinct Records");
            Console.WriteLine("--------------------");
            foreach (string dis in distinct)
            {
             Console.WriteLine("{0}", dis);
            }

       }

}




     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend