I have a question about Linq of C#.Based on the code of the ms site, I wrote a code that connects the two csv files with inner join and outputs them.
In the code, the output is from field 0 to field 4, but how do I output all fields?(There are more than 200 fields in total, so it is difficult to write them individually.)
Here is the code.Thank you for your cooperation.
using System;
using System.Linq;
using System.Collections.General;
class JoinStrings {
static void Main() {
string[]names=System.IO.File.ReadAllLines(@"C:\test\aa\names.csv", System.Text.Encoding.GetEncoding("Shift_JIS"));
string[]scores=System.IO.File.ReadAllLines(@"C:\test\aa\scores.csv", System.Text.Encoding.GetEncoding("Shift_JIS"));
IEnumerable<string>scoreQuery1=
from name in names
let nameFields = name.Split(',')
from id in scores
letscoreFields=id.Split(',')
where nameFields[0] == scoreFields[3]
select nameFields[0]+", "+scoreFields[1]+", "+scoreFields[2]
+ ", "+scoreFields[3]+", "+scoreFields[4];
OutputQueryResults(scoreQuery1, "Merget two spreadsheets:");
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit";
Console.ReadKey();
}
static void OutputQueryResults (IEnumerable <string > query, string message) {
Console.WriteLine (System.Environment.NewLine+message);
foreach (string item in query) {
Console.WriteLine(item);
}
Console.WriteLine("{0}total names in list", query.Count());
}
}
First, use String.Join() to create various bonds.However, since a single array or IEnumerable is required, it is recommended that you combine it with Enumerable.Concat().
varnames=from name in File.ReadLines(@"C:\test\aa\names.csv", Encoding.Default)
select name.Split(', ');
varscores=from score in File.ReadAllLines(@"C:\test\aa\scores.csv", Encoding.Default)
select score.Split(', ');
varscoreQuery1 = from name in names
join score in scores on name [0] equals score [3]
select String.Join(", ", new[]{name[0]}.Concat(score)));
Use EnumerableEx.StartWith() to make it a little cleaner.
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
918 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
578 Understanding How to Configure Google API Key
582 PHP ssh2_scp_send fails to send files as intended
620 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.