1
 var arrResults = Array1.Join(Array2, x => x.ID, x => x.ID, (first, second) => new
            {
                IDRecord = first.ID,
                Count1 = first.Count,
                Count2 = second.Count,
            })
            .OrderBy(item => item.IDRecord).ToArray(); 

            // bind & display results in datagrid
            dataGridView1.DataSource = arrResults;

My above code does exactly what I want it to, and shows the results in a dataGrid control. Now, I'd like to export the results to a text file, C:\output.txt, instead. How do I do this?

My previous attempts usually involve getting errors that say "cannot convert from 'string' to System.Collections.Generic.IEnumerable".

2 Answers 2

2

First convert the results to strings:

var lines = arrResults.Select(record => 
    record.ID + " " + record.Count1 + " " + record.Count2);//todo fix formatting

Then write them all to a file.

File.WriteAllLines("file.txt", lines);
Sign up to request clarification or add additional context in comments.

Comments

1

Use a System.IO.Streamwriter to write to file

using(StreamWriter sw = new StreamWriter(@"C:\output.txt")){
   foreach(var s in arrResults)
       sw.WriteLine(s);

}

See here for reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.