我正在处理CSV文件
说
ABC|06|001
PPP|06|001
我正在运行LINQ以拆分CSV
var path = Server.MapPath("~/App_Data/CSV.txt");
var _collectCSGData = from line in File.ReadAllLines(path)
let parts = line.Split('|')
select new { ID = parts[0],Assignment=parts[1]};
如何获取每行的最后一项?
(即)
001
001
答案 0 :(得分:8)
from line in File.ReadAllLines(path)
select line.Split('|').LastOrDefault()
答案 1 :(得分:1)
类似的东西:
parts[parts.length -1]
应该这样做。
答案 2 :(得分:1)
var _collectCSGData = from line in File.ReadAllLines(path)
let parts = line.Split('|')
let assignment = parts[parts.length - 1]
select assignment;
这应该有用,如果您需要按摩数据,let
是您的朋友。
<强>更新强>
由于部件可能是空的,您可以:
let assignment = parts.length > 0 ? parts[parts.length - 1] : String.Empty
答案 3 :(得分:0)
如果您知道这是第三部分,那么如何添加到您的匿名构造函数中:
var _collectCSGData = from line in File.ReadAllLines(path)
let parts = line.Split('|');
select new
{ID = parts[0], Assignment = parts[1], Data = parts[2]};
或者,如果它只是“最后一个项目,无论多少项目”
var _collectCSGData = from line in File.ReadAllLines(path)
let parts = line.Split('|');
select new
{ID = parts[0], Assignment = parts[1], Data = parts[part.length-1]};