如何使用Linq更改标题 [英] How to change the Headline using Linq

查看:77
本文介绍了如何使用Linq更改标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家我有这个代码,我用它来读取文件,然后将它们作为字符串放入IEnumerable。

我只是遇到问题,所有第一行应该是HeadLines但是在我的LineNumber列中我得到1作为名称有一种方法来改变它并写"RowNumber"相反?

Hi Guys I have this code and I use it to read files and then put them as strings into a IEnumerable.
I just have the problem that all first rows should be HeadLines but in my LineNumber column im getting 1 as the name is there a way to change that and write "RowNumber" instead?

var lines = File.ReadLines(readFolderFile, Encoding.Default)   
   .Select((line, index) => new { Line = line.Trim(';'), LineNumber = index + 1 })  
   .Where(x => !String.IsNullOrWhiteSpace(x.Line))  
   .Select(x => $"{x.LineNumber};{x.Line};");  




推荐答案

您好bkreye,

Hi bkreye,

请尝试以下LINQ。 

Please try the following LINQ. 

var lines = File.ReadLines(readFolderFile, Encoding.Default)
                           .Select((line, index) => new { Line = line.Trim(';'), LineNumber = index.ToString()  })
                           .Where(x => !String.IsNullOrWhiteSpace(x.Line))
                           .Select(x => string.Format("{0};{1};", x.LineNumber == "0" ? "RowNumber": x.LineNumber, x.Line)).ToList();

祝你好运,

张龙


这篇关于如何使用Linq更改标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆