5
Answers

Newbie need help understanding multi parameter lambda?

Photo of Ab Laxman

Ab Laxman

7y
264
1
Hi,
I'm new to coding and C# in general but I do know basics of the program. I also like LINQ but recently I came across a piece of code in the internet (on a website) 
  1. var matchCount = deqn.Value.Select((c, i) => deqn.Value.Substring(i)).Count(sub => sub.StartsWith("\\tag")); 
 where deqn is a xml node and the above code is to find how many times the string \tag appears in that node.
I can't figure out how the code works....I'm not familiar with multi parameter lambda expressions.
Can anyone explain (break it down) the code in details to me?

Answers (5)

2
Photo of Mukesh Nayak
NA 1.5k 317.4k 7y
Hi Laxman ,
 
Now there is 2 extension for the select . Please refer the image below : 
 
First extension in the image above is having one parameter  which is just element itself to be manipulated.
Now in the second case it is accepting  2 parameter where the first parameter is the element itself and second parameter is the index.
 
Now in you code using second parameter you are iterating through the all the value using substring. For example if your deqn.value = "\\tag SIGMA INSTITUTE" so it will iterate and get all the value such as 
\\tagSIGMA INSTITUTE    (i = 0)
tagSIGMA INSTITUTE (i=1)
agSigMA INSTITUTE (i = 3)
..
..
E (i = n)
 
then from all this element its checking where there \\tag  
 and getting count. 
 
 
Regards,
Mukesh Nayak 
1
Photo of Laxmidhar Sahoo
NA 2.3k 1.4k 7y
hi mukesh
The first is the iterated charecter
second is the position(index) of the iterated charecter 
 
1
Photo of Laxmidhar Sahoo
NA 2.3k 1.4k 7y
sir I made some changes with your code and bellow given a example .sir I look it and get back to me
  1. public ActionResult testdoublelambdacount()  
  2.       {
  3.           string strxml = "" +  
  4.                           "" +  
  5.                               " \\tag SIGMA INSTITUTE" +  
  6.                                "650" +  
  7.                          "" +  
  8.                          "" +  
  9.                               "ORCHID INSTITUTE" +  
  10.                                "1200" +  
  11.                           "" +  
  12.                           "";  
  13.           XmlDocument xDoc = new XmlDocument();  
  14.           xDoc.LoadXml(strxml);  
  15.           XmlNodeList xmlNodeList = xDoc.SelectNodes("/info/collage"); 
  16.           foreach (XmlNode xmlNode in xmlNodeList)  
  17.           {
  18.               string nodevalue = xmlNode["name"].InnerText;
  19.               var matchCount = nodevalue.Select((c, i) => nodevalue.Substring(i)).Count(sub => sub.StartsWith("\\tag")); 
  20.           }
  21.           return View();  
  22.       }  
 
0
Photo of Mukesh Nayak
NA 1.5k 317.4k 7y
Hi Laxman 
 
I modified the code something like  
  1. var matchCount = deqn.Select((c, i) => deqn.Substring(i) + " Iteration : " + i + " Element : " + c ).ToList();
and putting the break point  you will get the following : 
 
 So i = 0,  deqn.Substring(i)  = "\\tagSIGMA INSTITUTE", c ="",
      i = 1, deqn.Substring(i) = "\\tagSIGMA INSTITUTE", c ="\\",
      i = 2 , deqn.Substring(i) = "tagSIGMA INSTITUTE", c ="t",
                                       .........
                                       .........
 
Mukesh Nayak 
0
Photo of Ab Laxman
NA 9 281 7y
Hi Mukesh,
 Are the below strings values of "c" in the lambda?
  1. \\tagSIGMA INSTITUTE (i = 0)  
  2. tagSIGMA INSTITUTE (i=1)  
  3. agSigMA INSTITUTE (i = 3) 
or are they the value of "i"?
Sorry if this question sounds stupid but I'm a newby:)