Home Ask Login Register

Developers Planet

Your answer is one click away!

Erfercfsfr Erferf February 2016

Write to file List element of struct

I have such struct:

public struct ParsedUser
{
    public string Username;
    public string pk;
    public string RandPhotoId;
}

And I have filled List of ParsedUser:

List<ParsedUser> users;

How to write to file only Usernames?

Something like File.AppendAllLines(tag + ".txt", users.Select(x => new { x.Username} ));

Answers


Gusman February 2016

You were close:

File.AppendAllLines(tag + ".txt", string.Join("\r\n", users.Select(u => u.Username)));

First of all you don't need to create a new object, you want just the name, so select it plainly.

Second, ju need to join these strings with "\r\n", a newline plus carriage return, that is what the string.Join does.


Fabjan February 2016

You can simply use :

 var userNames = users.Select(x => x.Username).ToArray();
 File.AppendAllLines(tag + ".txt", userNames);

Method AppendAllLines create new line per every string in IEnumerable<string>.

Post Status

Asked in February 2016
Viewed 3,217 times
Voted 8
Answered 2 times

Search




Leave an answer


Quote of the day: live life