using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class MergeTwoCSVFiles
{
static void Main()
{
// See section Compiling the Code for information about the data files.
string[] names = System.IO.File.ReadAllLines(@"c:\users\mypc\documents\visual studio 2017\Projects" +
@"\CustomJoinsLinq_cs\CustomJoinsLinq_cs\names.csv");
string[] scores = System.IO.File.ReadAllLines(@"c:\users\mypc\documents\visual studio 2017\Projects" +
@"\CustomJoinsLinq_cs\CustomJoinsLinq_cs\scores.csv");
// Merge the data sources using a named type.
// You could use var instead of an explicit type for the query.
IEnumerable<Student> queryNamesScores =
// Split each line in the data files into an array of strings.
from name in names
let x = name.Split(',')
from score in scores
let s = score.Split(',')
// Look for matching IDs from the two data files.
where x[2] == s[0]
// If the IDs match, build a Student object.
select new Student()
{
FirstName = x[0],
LastName = x[1],
ID = Convert.ToInt32(x[2]),
ExamScores = (from scoreAsText in s.Skip(1)
select Convert.ToInt32(scoreAsText)).
ToList()
};
// Optional. Store the newly created student objects in memory
// for faster access in future queries
List<Student> students = queryNamesScores.ToList();
foreach (var student in students)
{
Console.WriteLine("The average score of {0} {1} is {2}.",
student.FirstName, student.LastName, student.ExamScores.Average());
}
/*foreach (var item in scores)
{
Console.WriteLine(item);
}*/
//Keep console window open in debug mode
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public List<int> ExamScores { get; set; }
}
嗨,大家好 请在运行上述程序时,只需按任意键即可退出。显示,这是程序的退出。它应该显示查询执行的结果,但事实并非如此。 在调试模式下,注意到获取的数据源(在上面的一个数据源上执行了每个数据源,并且工作正常),但是查询没有被创建,因此无法获得执行。 得分数组的字符串数据源在scores.csv文件中包含12行,但在调试模式下将得分数组的字符串添加到监视窗口,表示该数组长度为14.不知道这些是否有任何关系用它。 这两个类都在同一个命名空间,我没有在这里显示 请有人帮忙。我正在使用2017年的视觉工作室 干杯