Csharp/C#教程:如何在LINQ select中访问循环索引?分享


如何在LINQ select中访问循环索引?

这段代码:

var customers = from cust in Customers group cust by new {cust.Country} into grouping select new { Country = grouping.Key.Country, Customers = grouping }; customers.ToList().ForEach(g => Console.WriteLine("{0} has {1} customers: {2}", g.Country, g.Customers.Count(), String.Join(", ",g.Customers.Select(x => "#. " + x.CompanyName).ToArray()) )); customers.Dump(); 

产生这些结果:

 Argentina has 3 customers: #. Cactus Comidas para llevar, #. Océano Atlántico Ltda., #. Rancho grande Austria has 2 customers: #. Ernst Handel, #. Piccolo und mehr Belgium has 2 customers: #. Maison Dewey, #. Suprêmes délices Brazil has 9 customers: #. Comércio Mineiro, #. Familia Arquibaldo, #. Gourmet Lanchonetes, #. Hanari Carnes, #. Que Delícia, #. Queen Cozinha, #. Ricardo Adocicados, #. Tradição Hipermercados, #. Wellington Importadora Canada has 3 customers: #. Bottom-Dollar Markets, #. Laughing Bacchus Wine Cellars, #. Mère Paillarde Denmark has 2 customers: #. Simons bistro, #. Vaffeljernet ... 

如何用索引/计数替换“#”,以便得到如下结果:

 Argentina has 3 customers: 1. Cactus Comidas para llevar, 2. Océano Atlántico Ltda., 3. Rancho grande ... 

 customers.ToList().ForEach(g => Console.WriteLine("{0} has {1} customers: {2}", g.Country, g.Customers.Count(), string.Join(", ", g.Customers.Select((x, i) => i + ". " + x.CompanyName).ToArray()))); 

尝试制作一个int x = 0; 你的linq声明之前。 然后,在其中,print(x ++)。ToString()。

这将使xa闭包。

你可以这样做,以搜索迭代器索引值:

上述就是C#学习教程:如何在LINQ select中访问循环索引?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 var customers = from cust in Customers //Get Current Index let currIndex = Customers.IndexOf(cust) //You can then make use of the index in the Variable currIndex 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/956991.html

(0)
上一篇 2021年11月21日
下一篇 2021年11月21日

精彩推荐