C# 未创建Linq查询
嗨,伙计们 请在我运行上述程序时,只按任意键退出。显示,这是程序的出口。它应该显示查询执行的结果,但它没有 在调试模式下,注意到数据源在其中一个数据源上执行了一个for each(上面已注释掉)来获取数据,它工作正常,但是查询没有被创建,因此没有被执行。 字符串数据源的分数数组在scores.csv文件中包含12行,尽管在调试模式下添加字符串的分数数组以观察窗口,表示数组长度为14。不知道这些是否与此有关 这两个类都在同一个命名空间上,我在这里没有显示 请找个人帮忙。我正在使用visual studio 2017C# 未创建Linq查询,c#,linq,C#,Linq,嗨,伙计们 请在我运行上述程序时,只按任意键退出。显示,这是程序的出口。它应该显示查询执行的结果,但它没有 在调试模式下,注意到数据源在其中一个数据源上执行了一个for each(上面已注释掉)来获取数据,它工作正常,但是查询没有被创建,因此没有被执行。 字符串数据源的分数数组在scores.csv文件中包含12行,尽管在调试模式下添加字符串的分数数组以观察窗口,表示数组长度为14。不知道这些是否与此有关 这两个类都在同一个命名空间上,我在这里没有显示 请找个人帮忙。我正在使用visual st
干杯查询未创建是什么意思?哪个问题?你怎么知道的?对我来说,现在完全不清楚您的问题是什么。当然,正在创建查询。您的加入或数据中存在错误,导致无法返回任何数据。请确保文件包含正确的数据。行出现问题:其中x[2]==s[0]。名称文件中的第三列与分数文件中的第一列不匹配。@YeldarKurmangaliyev当我开始创建查询时,它会跳过它。不单步执行//从两个数据文件中查找匹配的ID。听起来你应该使用连接。。。
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; }
}