while循环中的变量初始化
我有一个可以分块读取文件的函数。
public static DataObject ReadNextFile(){ ...}
数据对象看起来像这样:
public DataObject
{
public string Category { get; set; }
// And other members ...
}
我想做的是以下基本上
List<DataObject> dataObjects = new List<DataObject>();
while(ReadNextFile().Category == "category")
{
dataObjects.Add(^^^^^ the thingy in the while);
}
我知道这可能不是它是如何完成的,因为我如何访问我刚刚读取的对象。
I have a function that reads a file in chunks.
public static DataObject ReadNextFile(){ ...}
And dataobject looks like this:
public DataObject
{
public string Category { get; set; }
// And other members ...
}
What I want to do is the following basically
List<DataObject> dataObjects = new List<DataObject>();
while(ReadNextFile().Category == "category")
{
dataObjects.Add(^^^^^ the thingy in the while);
}
I know it's probably not how it's done, because how do I access the object I've just read.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想你要找的是:
但我不会那样做。我会写:
其中
ReadItems()
是一种返回IEnumerable
的方法,一次读取并生成一项。您可能很想使用迭代器块(yield return
等)来实现它。这是假设您确实想在找到第一个具有不同类别的对象后立即停止阅读。如果您确实想要包含所有匹配的
DataObject
,将上述 LINQ 查询中的
TakeWhile
更改为Where
。(编辑:赛义德此后删除了他对答案的反对意见,但我想我不妨保留这个例子......)
编辑:证明这会起作用,因为赛义德似乎不相信我:
输出:
在其他中换句话说,该列表保留了对从
ReadNextFile
返回的 5 个不同对象的引用。I think what you're looking for is:
But I wouldn't do that. I'd write:
where
ReadItems()
was a method returning anIEnumerable<DataObject>
, reading and yielding one item at a time. You may well want to implement it with an iterator block (yield return
etc).This is assuming you really want to stop reading as soon as you find the first object which has a different category. If you actually want to include all the matching
DataObject
s,change
TakeWhile
toWhere
in the above LINQ query.(EDIT: Saeed has since deleted his objections to the answer, but I guess I might as well leave the example up...)
EDIT: Proof that this will work, as Saeed doesn't seem to believe me:
Output:
In other words, the list has retained references to the 5 distinct objects which have been returned from
ReadNextFile
.这是主观的,但我讨厌这种模式(并且我充分认识到我是这里的极少数)。当我需要这样的东西时,我是这样做的。
但现在,最好说
,当然,
GetItemsFromFile
从path
指向的文件中读取项目并返回IEnumerable< /代码>。
This is subjective, but I hate this pattern (and I fully recognize that I am in the very small minority here). Here is how I do it when I need something like this.
But these days, it is better to say
Here, of course,
GetItemsFromFile
reads the items from the file pointed to bypath
and returns anIEnumerable<DataObject>
.如果你有更复杂的对象,你可以这样做(比如 jon):
And if you have more complicated object you can do this (like jon):
您应该考虑在类容器上实现 IEnumerator 并调用 ReadNextFile()。然后,您将始终使用 IEnumerator.Current 引用当前对象,并且 MoveNext() 将返回您要查找的布尔值以检查进度。像这样的东西:
You should look into implementing IEnumerator on the class container the call to ReadNextFile(). Then you would always have reference to the current object with IEnumerator.Current, and MoveNext() will return the bool you are looking for to check for advancement. Something like this: