How to multiply the element of a list by another list without using zip LinQ?

FranK Duc 121 Reputation points
2021-11-30T15:15:26.663+00:00

Hello,

I am having an error message saying: sequence contains no elements.

Often it is link to LinQ.

How can i do the same as this var listM = lists.Zip(listv, (first, second) => first * second); without LinQ?

Thank you

Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2021-12-10T07:39:23.467+00:00

    @FranK Duc , Based on my test, I find that the code you provided can almost achieve your target.
    Tested code:

     var list1 = new List<double>();  
            var list2 = new List<int>();  
            list1.Add(2.11);  
            list1.Add(3.22);  
            list1.Add(4.36);  
            list1.Add(2.03);  
            list2.Add(12);  
            list2.Add(13);  
            list2.Add(16);  
            list2.Add(9);  
            list2.Add(6);  
            var list3 = new List<double>();  
      
            for (int i = 0; i < Math.Min(list1.Count,list2.Count); i++)  
            {  
                list3.Add(list1[i]*list2[i]);  
            }  
    

    Result:

    154014-image.png
    What is your current question about the code?

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. FranK Duc 121 Reputation points
    2021-11-30T15:44:33.397+00:00

    I find the way and it didn't solve my problem.

    For those who would like to know how to do it:

    var listD = new List<double>();
    
    for (int i = 0; i < Math.Min(listv.Count, lists.Count); i++)
          listD.Add(listv[i] * lists[i]); 
    
    foreach (var item in listD)
          Print("LISTD"+item);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.