Zom*_*man 3 c# linq csv text-files
我有一个简单的文本文件,其中包含一些具有以下结构的CSV:
Run Code Online (Sandbox Code Playgroud)@Parent1_Field1, Parent1_Field2, Parent1_Field3 Child1_Field1, Child1_Field2 Child2_Field1, Child2_Field2 ...etc. @Parent2_Field1, Parent2_Field2, Parent2_Field3 Child1_Field1, Child1_Field2 Child2_Field1, Child2_Field2 ...etc.
'@'表示紧邻其下方的子对象的父对象.(这可以使用XML更好地表示,但在我的情况下,这不是一个选项.)
我的目的是使用LINQ查询此文件而不将其整个内容加载到内存中.首先,我创建了一个实现IEnumerable的类(此处:MyCustomReader),其中我使用StreamReader获取文件的每一行.
例如,以下内容获取所有Parent对象(没有子对象):
from line in MyCustomReader
where line.StartsWith("@")
select Parent.Create(line)
Run Code Online (Sandbox Code Playgroud)
但是,当我想创建涉及Parent和Child对象的查询时,我陷入困境.例如,获取特定父对象的所有子对象或获取特定子字段包含相同值的所有父对象.
例如,这将获取特定Parent对象的所有子项:
public IEnumerable<Child> GetChildrenForAParent(string uniqueParentName)
{
Parent parent = null;
foreach (string line in MyCustomReader)
{
if (line.StartsWith("@"))
parent = Parent.Create(line);
else if (parent.UniqueName == uniqueParentName)
yield return Child.Create(line);
}
}
Run Code Online (Sandbox Code Playgroud)
和第二个例子:
public IEnumerable<Parent> GetParentsWhereChildHasThisValue(string childFiledValue)
{
Parent parent = null;
foreach (string line in MyCustomReader)
{
if (line.StartsWith("@"))
{
parent = Line.Create(line);
}
else //child
{
Child child = Child.Create(line);
if (child.FiledValue == childFiledValue)
yield return parent;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用LINQ实现这两个示例?
这不是很漂亮,但对于第一个类似下面的东西应该工作:
MyCustomReader.SkipWhile(line => line != uniqueParentName).Skip(1).
TakeWhile(line => !line.StartsWith("@"));
Run Code Online (Sandbox Code Playgroud)
编辑:好的,我很无聊.我认为这将为你做第二个(但显然它不是一个适合LINQ的问题):
var res = MyCustomReader.Where(parentLine => parentLine.StartsWith("@"))
.Join(MyCustomReader.Where(childLine => !childLine.StartsWith("@")),
parentLine => parentLine,
childLine => MyCustomReader.Reverse<string>()
.SkipWhile(z => z != childLine)
.SkipWhile(x => !x.StartsWith("@")).First(),
(x, y) => new { Parent = x, Child = y })
.Where(a => a.Child == childFiledValue).Select(a => a.Parent);
Run Code Online (Sandbox Code Playgroud)