gpt4 book ai didi

c# - 用于解析 CSV 文本行的 Linq Lookup

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:20 26 4
gpt4 key购买 nike

问题

我问过this question不久前,要求从那时起发生了一些变化。

现在,有可能有一个包含以下行的文件:

Bryar22053;ADDPWN;Bryar.Suarez@company.com;ACTIVE
Nicole49927;ADDPWN;Nicole.Acosta@company.com;ACTIVE
Rashad58323;ADDPWN;Rashad.Everett@company.com;ACTIVE

取第一行。跳过第一个值 Bryar22053 并使用相同的查找:

var columnCount = dataRow.Skip(1).Count();
var modular = 0;

// Simple Enum
var rightsFileType = new RightsFileType();

if (columnCount % 2 == 0)
{
rightsFileType = RightsFileType.WithoutStatus;
modular = 2;
}
else if (columnCount % 3 == 0)
{
rightsFileType = RightsFileType.WithStatus;
modular = 3;
}

var lookup = dataRow.Skip(1).Select((data, index) => new
{
lookup = index % modular,
index,
data
}).ToLookup(d => d.lookup);

查找对象现在有三组:

> ? lookup[0].ToList() Count = 1
> [0]: { lookup = 0, index = 0, data = "ADDPWN" } ? lookup[1].ToList() Count = 1
> [0]: { lookup = 1, index = 1, data = "Bryar.Suarez@company.com" } ? lookup[2].ToList() Count = 1
> [0]: { lookup = 2, index = 2, data = "ACTIVE" }

Debugging

如果是原来的情况,它只是 System1,User1,System2,User2... lookup 将有两个组,下面的代码将起作用:

List<RightObjectRetrieved> rights;
rights = lookup[0].Join(lookup[1], system => system.index + 1, username => username.index, (system, username) => new
{
system = system.data,
useraname = username.data
}).Where(d => !string.IsNullOrEmpty(d.system)).Select(d => new RightObjectRetrieved {UserIdentifier = userIdentifier, SystemIdentifer = d.system, Username = d.useraname, RightType = rightsFileType}).ToList();

// rights => Key = System Identifier, Value = Username

但是第三个“状态”为 System1,User1,Status1,System2,User2,Status2...,我在尝试加入和获取时遇到了问题三个全部。请帮忙。

编辑这是我的原始数据:

// Method has parameter localReadLine (string) that has this:
// Bryar22053;ADDPWN;Bryar.Suarez@company.com;ACTIVE

// Data line
var dataRow = localReadLine.Split(new[] { ToolSettings.RightsSeperator }, StringSplitOptions.None);

// Trim each element
Array.ForEach(dataRow, x => dataRow[Array.IndexOf(dataRow, x)] = x.Trim());

到目前为止已尝试(失败)

rights = lookup[0].Join(lookup[1], system => system.index + 1, username => username.index, status => status.index, (system, username, status) => new
{
system = system.data,
useraname = username.data,
status = status.data
}).Where(d => !string.IsNullOrEmpty(d.system)).Select(d => new RightObjectRetrieved {UserIdentifier = userIdentifier, SystemIdentifer = d.system, Username = d.useraname, RightType = rightsFileType}).ToList();

rights = lookup[0].Join(lookup[1], system => system.index + 1, username => username.index, (system, username) => new
{
system = system.data,
useraname = username.data
}).Join(lookup[2], status => status.index, (status) => new
{
status = status.data
}).Where(d => !string.IsNullOrEmpty(d.system)).Select(d => new RightObjectRetrieved {UserIdentifier = userIdentifier, SystemIdentifer = d.system, Username = d.useraname, RightType = rightsFileType, Status = ParseStatus(status)}).ToList();

最佳答案

我认为您需要稍微拆分一下您的实现。

让我们声明一个将保存数据的类:

class Data
{
public string System { get; set; }
public string Username { get; set; }
public string Status { get; set; }
}

现在,让我们定义几个解析函数来解析一行。第一个将解析包含状态的一行:

var withStatus = (IEnumerable<string> line) => line
.Select((token, index) => new { Value = token, Index = index })
.Aggregate(
new List<Data>(),
(list, token) =>
{
if( token.Index % 3 == 0 )
{
list.Add(new Data { System = token.Value });
return list;
}
var data = list.Last();
if( token.Index % 3 == 1 )
data.Username = token.Value;
else
data.Status = token.Value;
return list;
});

第二个将解析不包含状态的行:

var withoutStatus = (IEnumerable<string> line) => line
.Select((token, index) => new { Value = token, Index = index })
.Aggregate(new List<Data>(),
(list, token) =>
{
if( token.Index % 2 == 0)
list.Add(new Data { System = token.Value });
else
list.Last().Username = token.Value;
return list;
});

准备好所有这些后,您将需要:

  1. 确定模数
  2. 迭代文件的行并解析每一行
  3. 分组并汇总结果

剩下的代码看起来像这样:

var lines = streamReader.ReadAllLines(); // mind the system resources here!
var parser = lines.First().Split(';').Length % 2 == 0 ? withoutStatus : withStatus;
var data = lines.Skip(1) // skip the header
.Select(line =>
{
var parts = line.Split(';');
return new
{
UserId = parts.First(),
Data = parser(parts.Skip(1))
};
})
.GroupBy(x => x.UserId)
.ToDictionary(g => g.Key, g => g.SelectMany(x => x.Data));

现在你有一个 Dictionary<string, Data>其中包含用户 ID 及其信息。

当然,一个更优雅的解决方案是将每个解析函数分离到它自己的类中,并在一个公共(public)接口(interface)下加入这些类,以防将来要添加更多信息,但上面的代码应该可以工作并给你你应该做什么的想法。

关于c# - 用于解析 CSV 文本行的 Linq Lookup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32184419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com