Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have listA,listB Contains same count

C#
List<decimal> listA =new List<decimal>(){1.5M,2.63M,1.25M};

List<decimal> listB =new List<decimal>(){2.5M,0M,1M};


i need multiply listA*listB means

C#
1.5M*2.5M=3.75M
2.63M*0M=OM
1.25M*1M=1.25M


like above i need another List listC
how to do it please help.

i tried with listA[0]*listB[0];
is this correct but i have changed count at runtime
Posted
Updated 4-Oct-14 22:40pm
v3
Comments
Zoltán Zörgő 5-Oct-14 2:21am    
Quite simple task. What have you tried?
Abhi KA 5-Oct-14 2:38am    
just give me suggestion
Sergey Alexandrovich Kryukov 5-Oct-14 2:22am    
First of all, where is the definition of List?
It sounds way too trivial, so: what have you tried so far?
—SA
Abhi KA 5-Oct-14 2:26am    
sorry i defines list of decimal List<decimal> listA=new List<decimal>(){1.5M,2.63M,1.25M}; but in code not show why ?
Sergey Alexandrovich Kryukov 5-Oct-14 2:41am    
I did not ask you how you defined listA. I asked how did you define List (type); and you did not answer me. Not understanding the concept of definition would be a big problem.
—SA

C#
// uses Linq
using Linq

List<decimal> listA = new List<decimal>() { 1.5M, 2.63M, 1.25M };

List<decimal> listB = new List<decimal>() { 2.5M, 0M, 1M };

List<decimal> listC = listA.Select((dValue, index) => dValue * listB[index]).ToList();

This uses the form of Linq 'Select which automatically creates an index for you [^].

Note that this would fail if ListA had more elements than ListB, but work if ListA had fewer elements than ListB. You may want to put a test in your code to ensure both lists have the same number of elements ?
 
Share this answer
 
v2
How about this:
C#
var listC = new List<decimal>(listA.Zip(listB,(a,b)=>a*b));
Cheers
Andi
 
Share this answer
 
v2
One straightforward way to do this implementation is via a loop.
You have to do some validation first to check that both your input lists are of the same length, or if not decide rules for what to do in these cases.
For example:
C#
// Choose the shortest list of the two
// I am assuming here that your list has a Count property. 
// (As Sergey pointed out, the type of list is undefined)
int length = (listA.Count > listB.Count) ? listB.Count : listA.Count;

After this it is a simple matter to create listC and loop through the list items, multiply them and put them in listC.
 
Share this answer
 
Try this :

XML
List<decimal> listA = new List<decimal>() { 1.5M, 2.63M, 1.25M };
List<decimal> listB = new List<decimal>() { 1.5M, 2.63M, 1.25M };
List<decimal> listC = new List<decimal>(); ;
for (Int32 i = 0; i <= listA.Count; i++ )
{
    listC.Add( listA[i] * listB[i]);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900