You must Sign In to post a response.
  • Category: LINQ

    I want to sum of two variable in LINQ

    please tell me, how can i got sum of two variables in LINQ.

    please reply soon.


    Atul Sharma
  • #745740
    Hi Atul,

    Please post your code or describe your problem in detail. So that, we can guide you to add two variables in LINQ.

    Generally, LINQ is a query which we want to fetch a records from your data context. Based on your query, we can help you to add two variables in your LINQ query.

    I hope you are trying to add two variables in Lamda expression.

    V.M. Damodharan
    "Your talent will be worthless, when you have fear and tension."

  • #745744
    Hai Atul,
    Yes, you can get the sum of variables using the Linq.
    One if you have collection, then you can use that collection to retrieve the value and adds-up with other value to get sum of those two.

    int[] num = { 10, 14, 11, 31, 19, 18, 46, 17, 12, 10 };
    Console.WriteLine("Even numbers sum is {0} ", num.Sum( a=>(a%2==0) ));

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #745747
    You can perform the SUM operation using collection class as well instead of LINQ. Here I had made use of the list<>collection, refer the below code

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

    namespace ConsoleApplication2
    class Program
    static void Main(string[] args)
    List<int> values = new List<int>();
    for (int i = 0; i < 10; i++)

    int sumofTotal = values.Sum();

    All is well.
    Try and fail but don't fail and try

  • #745768
    If the value are stored in variable there is no need to make use of LINQ to find the sum.

    The LINQ can be used to find the some of some column from a data context or a Query.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #745926
    Hello you can use following code for addintion of 2 numbers from linq

    int iFirst = 10;
    int iSecond = 20;
    List<int> lstNumber = new List<int>();
    int iResult = lstNumber.Aggregate((item, items) => item + items);

    using this code you can calculate sum of n elements in list
    Thanks and Regards
    Umesh Bhosale

  • Sign In to post your comments