LINQ - Using LINQ - Asked By shruthi on 22-Jun-12 03:24 AM

Using LINQ, how to subtract two integers in the same list and store the result in another list?
Chintan Vaghela replied to shruthi on 22-Jun-12 03:57 AM

Hi Frndz,

 

Functionality:  Subtract List int Value and Display result in List

 

To achieve this task,

 

Subtract one value from other value and convert to list as following way

 

        var resultList = displayValue.Select(a => a.FirstValue - a.SecondValue).ToList();

 

 

Check below logic

 

 

 

 

 

Logic   :

 

Subtract second value from First and Dispaly in LIst

 

 

        var resultList = displayValue.Select(a => a.FirstValue - a.SecondValue).ToList();

 

 

 

 

Property with Two Int Value

public ClsProperty(int FirstValue, int SecondValue)

    {

      _FirstValue = FirstValue;

      _SecondValue = SecondValue;

    }

    private int _FirstValue;

    public int FirstValue

    {

      get { return _FirstValue; }

      set { _FirstValue = value; }

    }

    private int _SecondValue;

    public int  SecondValue

    {

      get { return _SecondValue; }

      set { _SecondValue = value; }

    }

Assign value to Property

 

List<ClsProperty> displayValue = new List<ClsProperty>();

        displayValue.Add(new ClsProperty(45, 120));

        displayValue.Add(new ClsProperty(12, 100));

        displayValue.Add(new ClsProperty(34, 132));

        displayValue.Add(new ClsProperty(56, 45));

 

 

Hope this helpful!

Thanks

 

 

 

shruthi replied to Chintan Vaghela on 22-Jun-12 05:06 AM

Thanks 4 d rply...

I've 2 lists :

lstIntegers. à Consists of Integer values

lstDifference -->Strores the result from  1st List.

 

i'm doin it in the below way :

for(int iCount=0;iCount<lsIntegers.Count;iCount++)

{

          lsDifference.Add(lsIntegers(iCount)-lsIntegers(iCount-1))

}

But it gives the performance issue n more over i dont want to use looping...
Can i achieve it using LINQ r some other way..?

Chintan Vaghela replied to shruthi on 22-Jun-12 05:27 AM
Linq is best way to avoid Looping and subtract two value  result get into Seperate List
shruthi replied to Chintan Vaghela on 22-Jun-12 05:29 AM
Can u please give me sample code to achieve it using LINQ.Thanks in advance...
Chintan Vaghela replied to shruthi on 22-Jun-12 05:31 AM
Try this way


var resultList = displayValue.Select(a => a.FirstValue - a.SecondValue).ToList();

 

shruthi replied to Chintan Vaghela on 22-Jun-12 05:36 AM
I dont have two property and i dont want to create new property also.
My list is something like this
List.Add(1);
List.Add(2);
List.Add(3);
List.Add(5);
List.Add(6);

And i want the sub of 2nd item from 1st item to be stored in other List.
Chintan Vaghela replied to shruthi on 22-Jun-12 07:21 AM

Hi Frndz,

 

Functionality:  Subract current and next row using LINQ

 

To achieve this task,

 

Make on IEnumerable for Get Next Value

 

private static T GetNext<T>(IEnumerable<T> list, T current)

    {

      try

      {

        return list.SkipWhile(x => !x.Equals(current)).Skip(1).First();

      }

      catch

      {

        return default(T);

      }

    }

 

 

 

Now  make your query as following way

 

var res = List.Select(i => GetNext<int>(List, i) - i);

 

 

 

Logic   :

 

 

List<int> List = new List<int>();

        List.Add(2);

        List.Add(5);

        List.Add(7);

 

        List.Add(11);

        List.Add(17);

        List.Add(0);

 

 

private static T GetNext<T>(IEnumerable<T> list, T current)

    {

      try

      {

        return list.SkipWhile(x => !x.Equals(current)).Skip(1).First();

      }

      catch

      {

        return default(T);

      }

    }

 

 

 

var res = List.Select(i => GetNext<int>(List, i) - i);

 

 

 

Hope this helpful!

Thanks

 

 

 

Chintan Vaghela replied to shruthi on 22-Jun-12 08:11 AM

Hi Frndz,

 

Functionality:  Integer ListItem Current row value subtract from next row Using LINQ (With out For loop)

 

To achieve this task,

 

First Added 0 inter end of list

 

After then  getting Current row value and index using  .Select( (value, index) =>

 

After then getting NextRow value based on Current index using

 

List.Select(i => i).Skip(index + 1).First()

 

After then subtract first value from second value and store to temp int Variable

 

int subtractValue = 0;

subtractValue = List.Select(i => i).Skip(index + 1).First() - value;

 

In Last Minus one from list item (because we have added one element)

 

.Take(List.Count() - 1).ToList()

 

 

 

Full Logic     :

 

List<int> List = new List<int>();

        List.Add(2);

        List.Add(5);

        List.Add(7);

        List.Add(7);

        List.Add(11);

        List.Add(17);

        List.Add(0);

        int subtractValue = 0;

        var resultList = List

               .Select(

               (value, index) =>

               {

                 subtractValue = List.Select(i => i).Skip(index + 1).First() - value;

                 return new

                  {

                    subtractValue

                  };

               }

              ).Take(List.Count() - 1).ToList();

 

 

Hope this helpful!

Thanks

 

 

 

shruthi replied to Chintan Vaghela on 25-Jun-12 05:11 AM
Thank U.....
Chintan Vaghela replied to shruthi on 25-Jun-12 06:32 AM
welcome   : )
shruthi replied to Chintan Vaghela on 26-Jun-12 05:27 AM
I've used this
    list.SkipWhile(x => !x.Equals(current)).Skip(1).First();
Its working gud... but i'm facing other problem. In my list some items are duplicated.
Example :
List<int> ls==> {1,2,3,3,4,5}
I want the output in another list as {(2-1),(3-2),(3-3),(4-3),(5-4)}
                        => {1,1,0,1,1}
but my output is giving as follows
                        => {1,1,0,0,1}

hw to handle dis?

Chintan Vaghela replied to shruthi on 26-Jun-12 07:58 AM

Hi Frndz,

 

Functionality:  Subtract Value

 

 

To achieve this task,

 

Change your logic as follows

 

 

 

Full Logic     :

 

 

List<int> List = new List<int>();

        List.Add(1);

        List.Add(2);

        List.Add(3);

        List.Add(3);

        List.Add(4);

        List.Add(5);

        List.Add(0);

        int subtractValue = 0;

var resultList = List

             .Select(

               (value, index) =>

               {

                 subtractValue = (List.Select(i => i).Skip(index + 1).First()) - value;

                 return new

                  {

                    subtractValue

                  };

               }

              ).Take(List.Count() - 2).ToList();

 

 

 

 

Hope this helpful!

Thanks

 

 

 

shruthi replied to Chintan Vaghela on 26-Jun-12 08:42 AM
Thanks 4 the rply...